UIScrollView extension that scrolls when the user tilts the device.
- When your view controller appears on screen (most cases in
viewWillAppearorviewDidAppear), callscrollView.setTiltingEnabled(true). - If at any point you want to stop the tilt-to-scroll, call
scrollView.setTiltingEnabled(false). This will automatically setscrollView.scrollingEnabled = true. - You can recalibrate the scroll view at any point by calling
scrollView.calibrate(). I recommend adding a button that allows the user to choose to recalibrate in order to redefine the angle at which the scroll view goes up and down.
/// Factor by which the accelerometer data is multiplied to scroll the view. Larger values cause faster scrolling.
public var tiltingFactor: CGFloat = 20/// Enables or disables the tilting behavior of the scroll view.
/// - true: Tilting the device causes the scroll view to scroll. `scrollingEnabled = false`
/// - false: Tilting the device does nothing. `scrollEnabled = true`
public func setTiltingEnabled(tiltingEnabled: Bool)/// Resets the internal calculations to use the current angle of the device as the reference point.
/// At the device's current angle, the scroll view will not scroll.
public func calibrate()If you have any issues or suggestions, please create an issue or a pull request (preferred :). Revisions and improvements are always welcome.
You can contact me on Twitter at @ERDekhayser.