How to make iPhone vibrate using Swift?

2019-01-12 16:41发布

I need to make the iPhone vibrate, but I don't know how to do that in Swift. I know that in Objective-C, you just write:

import AudioToolbox
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

But that is not working for me.

9条回答
小情绪 Triste *
2楼-- · 2019-01-12 17:40

Short example:

import UIKit
import AudioToolbox

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))            
    }
}

load onto your phone and it will vibrate. You can put it in a function or IBAction as you wish.

查看更多
老娘就宠你
3楼-- · 2019-01-12 17:41
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
查看更多
We Are One
4楼-- · 2019-01-12 17:42

In iOS 10 on iPhone 7 or 7 Plus, try:

let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
查看更多
登录 后发表回答