2015-12-13 42 views
0

我想使用搖晃功能來關閉鬧鐘,這會導致用戶晃動手機30秒。有沒有方法可以測試用戶晃動設備的時間以及如何設置抖動功能?搖一段時間?

我只明白swift代碼。

回答

0

你可以像下面這樣做

var timer = NSTimer() 

override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) { 
    if motion == .MotionShake { 
     print("Device shaken, shake timer started") 
     timer = NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: Selector("turnOffAlarm"), userInfo: nil, repeats: true) 
    } 
    else{ 
     timer.invalidate() 
    } 
} 

func turnOffAlarm(){ 
    print("Alarm off") 
    timer.invalidate() 
} 

當用戶開始搖晃手機您啓動計時器間隔30秒,如果用戶停止晃動,您將在30秒後調用invalidate,否則您將調用turnOffAlarm並對計時器進行評估。

0

我不確定你可以附加一個計時器來擺動手勢,但也許你可以設置一個計數器來觀察它觸發了多少次?

搭上擺動姿態,只是這種方法添加到您的視圖控制器:

override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) { 
    if motion == .MotionShake { 
     print("Device was shaken!") 
    } 
}