├── LICENSE ├── README.md ├── TiltingScrollView.swift ├── TiltingScrollView.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ │ └── TiltingScrollView.xcscmblueprint │ └── xcuserdata │ │ └── evan.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── evan.xcuserdatad │ └── xcschemes │ ├── TiltingScrollView.xcscheme │ └── xcschememanagement.plist └── TiltingScrollView ├── AppDelegate.swift ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── TiltingScrollView.swift └── ViewController.swift /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Evan Dekhayser 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TiltingScrollView 2 | ======== 3 | 4 | UIScrollView extension that scrolls when the user tilts the device. 5 | 6 | ## Usage 7 | 8 | - When your view controller appears on screen (most cases in `viewWillAppear` or `viewDidAppear`), call `scrollView.setTiltingEnabled(true)`. 9 | - If at any point you want to stop the tilt-to-scroll, call `scrollView.setTiltingEnabled(false)`. This will automatically set `scrollView.scrollingEnabled = true`. 10 | - 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. 11 | 12 | ## Public Variables 13 | 14 | ```swift 15 | /// Factor by which the accelerometer data is multiplied to scroll the view. Larger values cause faster scrolling. 16 | 17 | public var tiltingFactor: CGFloat = 20 18 | ``` 19 | 20 | ## Public Methods 21 | 22 | ```swift 23 | /// Enables or disables the tilting behavior of the scroll view. 24 | /// - true: Tilting the device causes the scroll view to scroll. `scrollingEnabled = false` 25 | /// - false: Tilting the device does nothing. `scrollEnabled = true` 26 | 27 | public func setTiltingEnabled(tiltingEnabled: Bool) 28 | ``` 29 | 30 | ```swift 31 | /// Resets the internal calculations to use the current angle of the device as the reference point. 32 | /// At the device's current angle, the scroll view will not scroll. 33 | 34 | public func calibrate() 35 | ``` 36 | 37 | ## Conclusion 38 | 39 | If you have any issues or suggestions, please create an issue or a pull request (preferred :). Revisions and improvements are always welcome. 40 | 41 | You can contact me on Twitter at [@ERDekhayser](https://twitter.com/ERDekhayser). 42 | -------------------------------------------------------------------------------- /TiltingScrollView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TiltingScrollView.swift 3 | // TiltingScrollView 4 | // 5 | // Created by Evan Dekhayser on 3/6/16. 6 | // Copyright © 2016 Evan Dekhayser. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import CoreMotion 11 | 12 | public class TiltingScrollView: UIScrollView { 13 | 14 | public var tiltingFactor: CGFloat = 20 15 | 16 | private var motionManager = CMMotionManager() 17 | 18 | private var initialAcceleration: Double! 19 | 20 | public func calibrate(){ 21 | if UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation){ 22 | initialAcceleration = motionManager.accelerometerData!.acceleration.y 23 | } else { 24 | initialAcceleration = motionManager.accelerometerData!.acceleration.x 25 | } 26 | } 27 | 28 | public func beginTiltToScroll(){ 29 | guard motionManager.accelerometerAvailable else { 30 | print("TiltingScrollView: Accelerometer is not available on this device.") 31 | return 32 | } 33 | scrollEnabled = false 34 | motionManager.accelerometerUpdateInterval = 0.001 35 | motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) { (data, error) in 36 | guard let data = data else { return } 37 | 38 | if self.initialAcceleration == nil{ 39 | self.calibrate() 40 | } 41 | 42 | let yAcceleration: Double 43 | if UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation){ 44 | yAcceleration = self.initialAcceleration - data.acceleration.y 45 | } else { 46 | yAcceleration = self.initialAcceleration - data.acceleration.x 47 | } 48 | 49 | let yOffset = self.tiltingFactor * CGFloat(yAcceleration) 50 | self.contentOffset = CGPoint(x: self.frame.origin.x, y: max(min(self.contentOffset.y - yOffset, self.contentSize.height - self.frame.size.height), 0)) 51 | } 52 | } 53 | 54 | public func setTiltingEnabled(tiltingEnabled: Bool){ 55 | if tiltingEnabled{ 56 | if !motionManager.accelerometerActive{ 57 | calibrate() 58 | beginTiltToScroll() 59 | } 60 | scrollEnabled = false 61 | } else { 62 | motionManager.stopAccelerometerUpdates() 63 | scrollEnabled = true 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /TiltingScrollView.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 2024B3A81C8C96CA00F2BB83 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2024B3A71C8C96CA00F2BB83 /* AppDelegate.swift */; }; 11 | 2024B3AA1C8C96CA00F2BB83 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2024B3A91C8C96CA00F2BB83 /* ViewController.swift */; }; 12 | 2024B3AD1C8C96CA00F2BB83 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2024B3AB1C8C96CA00F2BB83 /* Main.storyboard */; }; 13 | 2024B3AF1C8C96CA00F2BB83 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2024B3AE1C8C96CA00F2BB83 /* Assets.xcassets */; }; 14 | 2024B3B21C8C96CA00F2BB83 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2024B3B01C8C96CA00F2BB83 /* LaunchScreen.storyboard */; }; 15 | 2024B3BE1C8CCF7C00F2BB83 /* TiltingScrollView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2024B3BD1C8CCF7C00F2BB83 /* TiltingScrollView.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 2024B3A41C8C96CA00F2BB83 /* TiltingScrollView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TiltingScrollView.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | 2024B3A71C8C96CA00F2BB83 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | 2024B3A91C8C96CA00F2BB83 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 22 | 2024B3AC1C8C96CA00F2BB83 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 23 | 2024B3AE1C8C96CA00F2BB83 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | 2024B3B11C8C96CA00F2BB83 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 25 | 2024B3B31C8C96CA00F2BB83 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 26 | 2024B3BD1C8CCF7C00F2BB83 /* TiltingScrollView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TiltingScrollView.swift; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | 2024B3A11C8C96CA00F2BB83 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | 2024B39B1C8C96CA00F2BB83 = { 41 | isa = PBXGroup; 42 | children = ( 43 | 2024B3A61C8C96CA00F2BB83 /* TiltingScrollView */, 44 | 2024B3A51C8C96CA00F2BB83 /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | 2024B3A51C8C96CA00F2BB83 /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | 2024B3A41C8C96CA00F2BB83 /* TiltingScrollView.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | 2024B3A61C8C96CA00F2BB83 /* TiltingScrollView */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | 2024B3A71C8C96CA00F2BB83 /* AppDelegate.swift */, 60 | 2024B3A91C8C96CA00F2BB83 /* ViewController.swift */, 61 | 2024B3BD1C8CCF7C00F2BB83 /* TiltingScrollView.swift */, 62 | 2024B3AB1C8C96CA00F2BB83 /* Main.storyboard */, 63 | 2024B3AE1C8C96CA00F2BB83 /* Assets.xcassets */, 64 | 2024B3B01C8C96CA00F2BB83 /* LaunchScreen.storyboard */, 65 | 2024B3B31C8C96CA00F2BB83 /* Info.plist */, 66 | ); 67 | path = TiltingScrollView; 68 | sourceTree = ""; 69 | }; 70 | /* End PBXGroup section */ 71 | 72 | /* Begin PBXNativeTarget section */ 73 | 2024B3A31C8C96CA00F2BB83 /* TiltingScrollView */ = { 74 | isa = PBXNativeTarget; 75 | buildConfigurationList = 2024B3B61C8C96CA00F2BB83 /* Build configuration list for PBXNativeTarget "TiltingScrollView" */; 76 | buildPhases = ( 77 | 2024B3A01C8C96CA00F2BB83 /* Sources */, 78 | 2024B3A11C8C96CA00F2BB83 /* Frameworks */, 79 | 2024B3A21C8C96CA00F2BB83 /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = TiltingScrollView; 86 | productName = TiltingScrollView; 87 | productReference = 2024B3A41C8C96CA00F2BB83 /* TiltingScrollView.app */; 88 | productType = "com.apple.product-type.application"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | 2024B39C1C8C96CA00F2BB83 /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 0720; 97 | LastUpgradeCheck = 0720; 98 | ORGANIZATIONNAME = "Evan Dekhayser"; 99 | TargetAttributes = { 100 | 2024B3A31C8C96CA00F2BB83 = { 101 | CreatedOnToolsVersion = 7.2.1; 102 | }; 103 | }; 104 | }; 105 | buildConfigurationList = 2024B39F1C8C96CA00F2BB83 /* Build configuration list for PBXProject "TiltingScrollView" */; 106 | compatibilityVersion = "Xcode 3.2"; 107 | developmentRegion = English; 108 | hasScannedForEncodings = 0; 109 | knownRegions = ( 110 | en, 111 | Base, 112 | ); 113 | mainGroup = 2024B39B1C8C96CA00F2BB83; 114 | productRefGroup = 2024B3A51C8C96CA00F2BB83 /* Products */; 115 | projectDirPath = ""; 116 | projectRoot = ""; 117 | targets = ( 118 | 2024B3A31C8C96CA00F2BB83 /* TiltingScrollView */, 119 | ); 120 | }; 121 | /* End PBXProject section */ 122 | 123 | /* Begin PBXResourcesBuildPhase section */ 124 | 2024B3A21C8C96CA00F2BB83 /* Resources */ = { 125 | isa = PBXResourcesBuildPhase; 126 | buildActionMask = 2147483647; 127 | files = ( 128 | 2024B3B21C8C96CA00F2BB83 /* LaunchScreen.storyboard in Resources */, 129 | 2024B3AF1C8C96CA00F2BB83 /* Assets.xcassets in Resources */, 130 | 2024B3AD1C8C96CA00F2BB83 /* Main.storyboard in Resources */, 131 | ); 132 | runOnlyForDeploymentPostprocessing = 0; 133 | }; 134 | /* End PBXResourcesBuildPhase section */ 135 | 136 | /* Begin PBXSourcesBuildPhase section */ 137 | 2024B3A01C8C96CA00F2BB83 /* Sources */ = { 138 | isa = PBXSourcesBuildPhase; 139 | buildActionMask = 2147483647; 140 | files = ( 141 | 2024B3AA1C8C96CA00F2BB83 /* ViewController.swift in Sources */, 142 | 2024B3A81C8C96CA00F2BB83 /* AppDelegate.swift in Sources */, 143 | 2024B3BE1C8CCF7C00F2BB83 /* TiltingScrollView.swift in Sources */, 144 | ); 145 | runOnlyForDeploymentPostprocessing = 0; 146 | }; 147 | /* End PBXSourcesBuildPhase section */ 148 | 149 | /* Begin PBXVariantGroup section */ 150 | 2024B3AB1C8C96CA00F2BB83 /* Main.storyboard */ = { 151 | isa = PBXVariantGroup; 152 | children = ( 153 | 2024B3AC1C8C96CA00F2BB83 /* Base */, 154 | ); 155 | name = Main.storyboard; 156 | sourceTree = ""; 157 | }; 158 | 2024B3B01C8C96CA00F2BB83 /* LaunchScreen.storyboard */ = { 159 | isa = PBXVariantGroup; 160 | children = ( 161 | 2024B3B11C8C96CA00F2BB83 /* Base */, 162 | ); 163 | name = LaunchScreen.storyboard; 164 | sourceTree = ""; 165 | }; 166 | /* End PBXVariantGroup section */ 167 | 168 | /* Begin XCBuildConfiguration section */ 169 | 2024B3B41C8C96CA00F2BB83 /* Debug */ = { 170 | isa = XCBuildConfiguration; 171 | buildSettings = { 172 | ALWAYS_SEARCH_USER_PATHS = NO; 173 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 174 | CLANG_CXX_LIBRARY = "libc++"; 175 | CLANG_ENABLE_MODULES = YES; 176 | CLANG_ENABLE_OBJC_ARC = YES; 177 | CLANG_WARN_BOOL_CONVERSION = YES; 178 | CLANG_WARN_CONSTANT_CONVERSION = YES; 179 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 180 | CLANG_WARN_EMPTY_BODY = YES; 181 | CLANG_WARN_ENUM_CONVERSION = YES; 182 | CLANG_WARN_INT_CONVERSION = YES; 183 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 184 | CLANG_WARN_UNREACHABLE_CODE = YES; 185 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 186 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 187 | COPY_PHASE_STRIP = NO; 188 | DEBUG_INFORMATION_FORMAT = dwarf; 189 | ENABLE_STRICT_OBJC_MSGSEND = YES; 190 | ENABLE_TESTABILITY = YES; 191 | GCC_C_LANGUAGE_STANDARD = gnu99; 192 | GCC_DYNAMIC_NO_PIC = NO; 193 | GCC_NO_COMMON_BLOCKS = YES; 194 | GCC_OPTIMIZATION_LEVEL = 0; 195 | GCC_PREPROCESSOR_DEFINITIONS = ( 196 | "DEBUG=1", 197 | "$(inherited)", 198 | ); 199 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 200 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 201 | GCC_WARN_UNDECLARED_SELECTOR = YES; 202 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 203 | GCC_WARN_UNUSED_FUNCTION = YES; 204 | GCC_WARN_UNUSED_VARIABLE = YES; 205 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 206 | MTL_ENABLE_DEBUG_INFO = YES; 207 | ONLY_ACTIVE_ARCH = YES; 208 | SDKROOT = iphoneos; 209 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 210 | TARGETED_DEVICE_FAMILY = "1,2"; 211 | }; 212 | name = Debug; 213 | }; 214 | 2024B3B51C8C96CA00F2BB83 /* Release */ = { 215 | isa = XCBuildConfiguration; 216 | buildSettings = { 217 | ALWAYS_SEARCH_USER_PATHS = NO; 218 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 219 | CLANG_CXX_LIBRARY = "libc++"; 220 | CLANG_ENABLE_MODULES = YES; 221 | CLANG_ENABLE_OBJC_ARC = YES; 222 | CLANG_WARN_BOOL_CONVERSION = YES; 223 | CLANG_WARN_CONSTANT_CONVERSION = YES; 224 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 225 | CLANG_WARN_EMPTY_BODY = YES; 226 | CLANG_WARN_ENUM_CONVERSION = YES; 227 | CLANG_WARN_INT_CONVERSION = YES; 228 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 229 | CLANG_WARN_UNREACHABLE_CODE = YES; 230 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 231 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 232 | COPY_PHASE_STRIP = NO; 233 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 234 | ENABLE_NS_ASSERTIONS = NO; 235 | ENABLE_STRICT_OBJC_MSGSEND = YES; 236 | GCC_C_LANGUAGE_STANDARD = gnu99; 237 | GCC_NO_COMMON_BLOCKS = YES; 238 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 239 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 240 | GCC_WARN_UNDECLARED_SELECTOR = YES; 241 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 242 | GCC_WARN_UNUSED_FUNCTION = YES; 243 | GCC_WARN_UNUSED_VARIABLE = YES; 244 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 245 | MTL_ENABLE_DEBUG_INFO = NO; 246 | SDKROOT = iphoneos; 247 | TARGETED_DEVICE_FAMILY = "1,2"; 248 | VALIDATE_PRODUCT = YES; 249 | }; 250 | name = Release; 251 | }; 252 | 2024B3B71C8C96CA00F2BB83 /* Debug */ = { 253 | isa = XCBuildConfiguration; 254 | buildSettings = { 255 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 256 | INFOPLIST_FILE = TiltingScrollView/Info.plist; 257 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 258 | PRODUCT_BUNDLE_IDENTIFIER = com.xappox.TiltingScrollView; 259 | PRODUCT_NAME = "$(TARGET_NAME)"; 260 | }; 261 | name = Debug; 262 | }; 263 | 2024B3B81C8C96CA00F2BB83 /* Release */ = { 264 | isa = XCBuildConfiguration; 265 | buildSettings = { 266 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 267 | INFOPLIST_FILE = TiltingScrollView/Info.plist; 268 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 269 | PRODUCT_BUNDLE_IDENTIFIER = com.xappox.TiltingScrollView; 270 | PRODUCT_NAME = "$(TARGET_NAME)"; 271 | }; 272 | name = Release; 273 | }; 274 | /* End XCBuildConfiguration section */ 275 | 276 | /* Begin XCConfigurationList section */ 277 | 2024B39F1C8C96CA00F2BB83 /* Build configuration list for PBXProject "TiltingScrollView" */ = { 278 | isa = XCConfigurationList; 279 | buildConfigurations = ( 280 | 2024B3B41C8C96CA00F2BB83 /* Debug */, 281 | 2024B3B51C8C96CA00F2BB83 /* Release */, 282 | ); 283 | defaultConfigurationIsVisible = 0; 284 | defaultConfigurationName = Release; 285 | }; 286 | 2024B3B61C8C96CA00F2BB83 /* Build configuration list for PBXNativeTarget "TiltingScrollView" */ = { 287 | isa = XCConfigurationList; 288 | buildConfigurations = ( 289 | 2024B3B71C8C96CA00F2BB83 /* Debug */, 290 | 2024B3B81C8C96CA00F2BB83 /* Release */, 291 | ); 292 | defaultConfigurationIsVisible = 0; 293 | defaultConfigurationName = Release; 294 | }; 295 | /* End XCConfigurationList section */ 296 | }; 297 | rootObject = 2024B39C1C8C96CA00F2BB83 /* Project object */; 298 | } 299 | -------------------------------------------------------------------------------- /TiltingScrollView.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /TiltingScrollView.xcodeproj/project.xcworkspace/xcshareddata/TiltingScrollView.xcscmblueprint: -------------------------------------------------------------------------------- 1 | { 2 | "DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "0EB9B442BFF22E7391F1FFAF6413F62B8FE8EFD4", 3 | "DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : { 4 | 5 | }, 6 | "DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : { 7 | "4B4BAACA98DEB00554F1C47B8448075ADE3181B2" : 0, 8 | "0EB9B442BFF22E7391F1FFAF6413F62B8FE8EFD4" : 0 9 | }, 10 | "DVTSourceControlWorkspaceBlueprintIdentifierKey" : "FD19E9A1-B9EB-4226-92AE-6BEBF3CBAB1D", 11 | "DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : { 12 | "4B4BAACA98DEB00554F1C47B8448075ADE3181B2" : "", 13 | "0EB9B442BFF22E7391F1FFAF6413F62B8FE8EFD4" : "TiltingScrollView\/" 14 | }, 15 | "DVTSourceControlWorkspaceBlueprintNameKey" : "TiltingScrollView", 16 | "DVTSourceControlWorkspaceBlueprintVersion" : 204, 17 | "DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "TiltingScrollView.xcodeproj", 18 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [ 19 | { 20 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/edekhayser\/TiltingScrollView.git", 21 | "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", 22 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "0EB9B442BFF22E7391F1FFAF6413F62B8FE8EFD4" 23 | }, 24 | { 25 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/edekhayser\/UITableView-RowConvenience.git", 26 | "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", 27 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "4B4BAACA98DEB00554F1C47B8448075ADE3181B2" 28 | } 29 | ] 30 | } -------------------------------------------------------------------------------- /TiltingScrollView.xcodeproj/project.xcworkspace/xcuserdata/evan.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edekhayser/TiltingScrollView/28b3ea956d82b6edd12c01f5a228195a07703556/TiltingScrollView.xcodeproj/project.xcworkspace/xcuserdata/evan.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /TiltingScrollView.xcodeproj/xcuserdata/evan.xcuserdatad/xcschemes/TiltingScrollView.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /TiltingScrollView.xcodeproj/xcuserdata/evan.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | TiltingScrollView.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 2024B3A31C8C96CA00F2BB83 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /TiltingScrollView/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // TiltingScrollView 4 | // 5 | // Created by Evan Dekhayser on 3/6/16. 6 | // Copyright © 2016 Evan Dekhayser. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /TiltingScrollView/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /TiltingScrollView/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /TiltingScrollView/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /TiltingScrollView/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /TiltingScrollView/TiltingScrollView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TiltingScrollView.swift 3 | // TiltingScrollView 4 | // 5 | // Created by Evan Dekhayser on 3/6/16. 6 | // Copyright © 2016 Evan Dekhayser. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import CoreMotion 11 | 12 | public extension UIScrollView { 13 | 14 | private struct AssociatedKeys { 15 | static var TiltingFactor = "tsv_TiltingFactor" 16 | 17 | static var MotionManager = "tsv_MotionManager" 18 | static var InitialAcceleration = "tsv_InitialAcceleration" 19 | } 20 | 21 | // MARK: Public Variables 22 | 23 | /// Factor by which the accelerometer data is multiplied to scroll the view. Larger values cause faster scrolling. 24 | 25 | public var tiltingFactor: CGFloat { 26 | get { 27 | return CGFloat((objc_getAssociatedObject(self, &AssociatedKeys.TiltingFactor) as? NSNumber ?? 20).floatValue) 28 | } 29 | set { 30 | objc_setAssociatedObject(self, &AssociatedKeys.TiltingFactor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 31 | } 32 | } 33 | 34 | // MARK: Private Variables 35 | 36 | private var motionManager: CMMotionManager { 37 | get { 38 | let motionManager = objc_getAssociatedObject(self, &AssociatedKeys.MotionManager) as? CMMotionManager 39 | if let motionManager = motionManager{ 40 | return motionManager 41 | } else { 42 | objc_setAssociatedObject(self, &AssociatedKeys.MotionManager, CMMotionManager(), .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 43 | return objc_getAssociatedObject(self, &AssociatedKeys.MotionManager) as! CMMotionManager 44 | } 45 | } 46 | set { 47 | objc_setAssociatedObject(self, &AssociatedKeys.TiltingFactor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 48 | } 49 | } 50 | 51 | private var initialAcceleration: Double! { 52 | get { 53 | return (objc_getAssociatedObject(self, &AssociatedKeys.InitialAcceleration) as? NSNumber)?.doubleValue 54 | } 55 | set { 56 | objc_setAssociatedObject(self, &AssociatedKeys.InitialAcceleration, NSNumber(double: newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 57 | } 58 | } 59 | 60 | // MARK: Public Methods 61 | 62 | /// Enables or disables the tilting behavior of the scroll view. 63 | /// - true: Tilting the device causes the scroll view to scroll. `scrollingEnabled = false` 64 | /// - false: Tilting the device does nothing. `scrollEnabled = true` 65 | 66 | public func setTiltingEnabled(tiltingEnabled: Bool){ 67 | if tiltingEnabled{ 68 | if !motionManager.accelerometerActive{ 69 | calibrate() 70 | beginTiltToScroll() 71 | } 72 | scrollEnabled = false 73 | } else { 74 | motionManager.stopAccelerometerUpdates() 75 | scrollEnabled = true 76 | } 77 | } 78 | 79 | /// Resets the internal calculations to use the current angle of the device as the reference point. 80 | /// At the device's current angle, the scroll view will not scroll. 81 | 82 | public func calibrate(){ 83 | guard let data = motionManager.accelerometerData else { return } 84 | if UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation){ 85 | initialAcceleration = data.acceleration.y 86 | } else { 87 | initialAcceleration = data.acceleration.x 88 | } 89 | } 90 | 91 | // MARK: Private Methods 92 | 93 | private func beginTiltToScroll(){ 94 | guard motionManager.accelerometerAvailable else { 95 | print("TiltingScrollView: Accelerometer is not available on this device.") 96 | return 97 | } 98 | scrollEnabled = false 99 | motionManager.accelerometerUpdateInterval = 0.001 100 | motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) { (data, error) in 101 | guard let data = data else { return } 102 | 103 | if self.initialAcceleration == nil{ 104 | self.calibrate() 105 | } 106 | 107 | let yAcceleration: Double 108 | if UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation){ 109 | yAcceleration = self.initialAcceleration - data.acceleration.y 110 | } else { 111 | yAcceleration = self.initialAcceleration - data.acceleration.x 112 | } 113 | print(yAcceleration) 114 | 115 | let shouldOffsetBeFlipped = UIApplication.sharedApplication().statusBarOrientation == .LandscapeLeft || UIApplication.sharedApplication().statusBarOrientation == .PortraitUpsideDown 116 | 117 | let yOffset = self.tiltingFactor * CGFloat(yAcceleration) * (shouldOffsetBeFlipped ? -1 : 1) 118 | self.contentOffset = CGPoint(x: self.frame.origin.x, y: max(min(self.contentOffset.y - yOffset, self.contentSize.height - self.frame.size.height), 0)) 119 | } 120 | } 121 | 122 | } 123 | -------------------------------------------------------------------------------- /TiltingScrollView/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // TiltingScrollView 4 | // 5 | // Created by Evan Dekhayser on 3/6/16. 6 | // Copyright © 2016 Evan Dekhayser. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | @IBOutlet weak var scrollView: UIScrollView! 14 | @IBOutlet weak var calibrateButton: UIButton! 15 | 16 | override func viewDidAppear(animated: Bool) { 17 | super.viewDidAppear(animated) 18 | scrollView.setTiltingEnabled(true) 19 | } 20 | 21 | @IBAction func calibrate(sender: UIButton) { 22 | scrollView.calibrate() 23 | 24 | calibrateButton.setTitle("Calibrated 🎉", forState: .Normal) 25 | let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))) 26 | dispatch_after(delayTime, dispatch_get_main_queue()) { 27 | self.calibrateButton.setTitle("Calibrate", forState: .Normal) 28 | } 29 | } 30 | } 31 | 32 | --------------------------------------------------------------------------------