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条回答
放荡不羁爱自由
2楼-- · 2019-01-12 17:19

For iOS 10.0+ You can try UIFeedbackGenerator

Simple viewController above, just replace your view controller in your test "single view app"

import UIKit

class ViewController: UIViewController {

    var i = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        let btn = UIButton()
        self.view.addSubview(btn)
        btn.translatesAutoresizingMaskIntoConstraints = false

        btn.widthAnchor.constraint(equalToConstant: 160).isActive = true
        btn.heightAnchor.constraint(equalToConstant: 160).isActive = true
        btn.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        btn.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

        btn.setTitle("Tap me!", for: .normal)
        btn.setTitleColor(UIColor.red, for: .normal)
        btn.addTarget(self, action: #selector(tapped), for: .touchUpInside)
    }

    @objc func tapped() {
        i += 1
        print("Running \(i)")

        switch i {
        case 1:
            let generator = UINotificationFeedbackGenerator()
            generator.notificationOccurred(.error)

        case 2:
            let generator = UINotificationFeedbackGenerator()
            generator.notificationOccurred(.success)

        case 3:
            let generator = UINotificationFeedbackGenerator()
            generator.notificationOccurred(.warning)

        case 4:
            let generator = UIImpactFeedbackGenerator(style: .light)
            generator.impactOccurred()
        case 5:
            let generator = UIImpactFeedbackGenerator(style: .medium)
            generator.impactOccurred()

        case 6:
            let generator = UIImpactFeedbackGenerator(style: .heavy)
            generator.impactOccurred()

        default:
            let generator = UISelectionFeedbackGenerator()
            generator.selectionChanged()
            i = 0
        }
    }
}
查看更多
戒情不戒烟
3楼-- · 2019-01-12 17:21

Here you will find all codes for each .caf and associated category :

https://github.com/TUNER88/iOSSystemSoundsLibrary

For example if you want a lighter vibration you can use the code 1003.

Good luck and have fun ;)

查看更多
做自己的国王
4楼-- · 2019-01-12 17:27

Swift 4.2 Updated

Just insert code below into your project.

Usage

Vibration.success.vibrate()

Source Code

enum Vibration {
case error
case success
case warning
case light
case medium
case heavy
case selection
case oldSchool

func vibrate() {

  switch self {
  case .error:
    let generator = UINotificationFeedbackGenerator()
    generator.notificationOccurred(.error)

  case .success:
    let generator = UINotificationFeedbackGenerator()
    generator.notificationOccurred(.success)

  case .warning:
    let generator = UINotificationFeedbackGenerator()
    generator.notificationOccurred(.warning)

  case .light:
    let generator = UIImpactFeedbackGenerator(style: .light)
    generator.impactOccurred()

  case .medium:
    let generator = UIImpactFeedbackGenerator(style: .medium)
    generator.impactOccurred()

  case .heavy:
    let generator = UIImpactFeedbackGenerator(style: .heavy)
    generator.impactOccurred()

  case .selection:
    let generator = UISelectionFeedbackGenerator()
    generator.selectionChanged()

  case .oldSchool:
    AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
  }

}

}
查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-12 17:31

We can do this in Xcode7.1

import UIKit
import AudioToolbox


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
    }
}
查看更多
神经病院院长
6楼-- · 2019-01-12 17:34

Other vibration types:

import AudioToolbox

AudioServicesPlaySystemSound(1519) // Actuate "Peek" feedback (weak boom)
AudioServicesPlaySystemSound(1520) // Actuate "Pop" feedback (strong boom)
AudioServicesPlaySystemSound(1521) // Actuate "Nope" feedback (series of three weak booms)

More About Vibration - http://www.mikitamanko.com/blog/2017/01/29/haptic-feedback-with-uifeedbackgenerator/

查看更多
不美不萌又怎样
7楼-- · 2019-01-12 17:35
import AudioToolbox

extension UIDevice {
    static func vibrate() {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
    }
}

Now you can just call UIDevice.vibrate() as needed.

查看更多
登录 后发表回答