Skip to content

Instantly share code, notes, and snippets.

@hungtruong
Created December 3, 2017 17:27
Show Gist options
  • Save hungtruong/3e51567235cff2c08c345cb933a954db to your computer and use it in GitHub Desktop.
Save hungtruong/3e51567235cff2c08c345cb933a954db to your computer and use it in GitHub Desktop.
/// This method will start a timer to either "wake" the device or put it to sleep if it is in the right orientation.
/// On a timer because the real device seems to delay it by a bit.
func handleDeviceMotion(motion: CMDeviceMotion) {
// device is tilted up
if motion.attitude.pitch > 0.5 {
if tiltActionTimer == nil {
tiltActionTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false, block: { (_) in
if self.passcodeViewMode == .inactive {
self.setPasscodeViewMode(.lockedScreen)
}
self.tiltActionTimer = nil
})
}
} else { //device is tilted down
if passcodeViewMode == .lockedScreen {
//delay switching to inactive mode for a sec
if tiltActionTimer == nil {
tiltActionTimer = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false, block: { (_) in
if self.passcodeViewMode == .lockedScreen { //we could've gone to the passcode entry at this point
self.setPasscodeViewMode(.inactive)
}
self.tiltActionTimer = nil
})
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment