2017-10-10 81 views
0

我在xcode中创建了一个应用程序,该应用程序永远随着按钮振动,并停止使用按钮,但是当我在iPhone上测试它时,它不会振动。帮帮我?我的swift代码不会执行振动

var timer: Timer? 


@IBAction func button1(_ sender: UIButton) { 

    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) 
    timer?.invalidate() 
    timer = Timer.scheduledTimer(timeInterval: 0.0, target: self, selector: Selector(("doaction")), userInfo: nil, repeats: true) 

} 

@IBAction func button2(_ sender: UIButton) { 

    timer?.invalidate() 
    timer = nil 

} 

我也使用iPhone 7 plus,如果这有所作为。

+0

确保您的移动振动不会被设置为关闭做呢?它仅在移动振动打开时起作用。 –

+0

我该如何开启它? –

+0

设置 - >声音 - >你必须振动 –

回答

0

AudioServicesPlaySystemSound替换为AudioServicesPlayAlertSound。另外,请确保您正在导入AudioToolbox

如果你想使用AudioServicesPlaySystemSound,你有如下

AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))