Swift CoreMotion detect Tap or Knock on device while in background

旧时模样 提交于 2019-12-20 10:12:05

问题


I am currently in the Process of building my first iOS Swift app, with this app I want to perform an action while the app is running in the background.

The action needs to be performed once the user taps twice on the device.

I've enabled Background Modes: Location updates in the app Capabilities

And setup a AccelerometerUpdatesToQueue function in the AppDelegate:

let manager = CMMotionManager()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

if manager.accelerometerAvailable {
    manager.accelerometerUpdateInterval = 0.01
    manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
        [weak self] (data: CMAccelerometerData!, error: NSError!) in

        println(data.acceleration.z)
    }
}

     return true
}

The console prints out the acceleration.z value as expected, but once I press the home button, the console prints stop.

I've searched the web for a sample code on how to do this, and I know it's possible... because we all know the app "Knock Knock to unlock", but I can't seem to find a piece of sample code for Swift.


回答1:


I got it to work! This is my solution, feel free to suggest improvements :)

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    let manager = CMMotionManager()
    var knocked : Bool = false
    let motionUpdateInterval : Double = 0.05
    var knockReset : Double = 2.0

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        if manager.deviceMotionAvailable {
            manager.deviceMotionUpdateInterval = motionUpdateInterval // seconds

            manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
                [weak self] (data: CMDeviceMotion!, error: NSError!) in


                if (data.userAcceleration.z < -0.7) || (data.userAcceleration.z > 0.7) { // Check if device is knocked

                    // Check for double knock
                    if self!.knocked == false {
                        // First knock
                        println("First Knock")
                        self!.knocked = true

                    }else{
                        // Second knock
                        println("Double Knocked")
                        self!.knocked = false
                        self!.knockReset = 2.0

                        // Action:
                    }
                }

                // Countdown for reset action (second knock must be within the knockReset limit)
                if (self!.knocked) && (self!.knockReset >= 0.0) {

                    self!.knockReset = self!.knockReset - self!.motionUpdateInterval

                }else if self!.knocked == true {
                    self!.knocked = false
                    self!.knockReset = 2.0
                    println("Reset")
                }

            }
        }

        return true
    }



回答2:


Thanks for posting this. It's helping me out though I need to figure out how to separate this data from regular movement data. One suggestion... use fabs to get the absolute value of userAcceleration.z.

 if (data.userAcceleration.z < -0.7) || (data.userAcceleration.z > 0.7) { // Check if device is knocked

becomes

if (fabs(data.userAcceleration.z) > 0.7) { // Check if device is knocked


来源:https://stackoverflow.com/questions/30619778/swift-coremotion-detect-tap-or-knock-on-device-while-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!