如何使用 Swift 使 iPhone 震动?

我需要让 iPhone 震动,但我不知道在 Swift 里怎么做。我知道在 Objective-C 中,你只需要写:

import AudioToolbox
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

但这对我不起作用。

99200 次浏览

简短的例子:

import UIKit
import AudioToolbox


class ViewController: UIViewController {


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

加载到你的手机上,它就会震动。你可以把它放在一个函数或 IBAction,如你所愿。

代码更新:

 AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { }

正如苹果代码文档所写:

此函数将在以后的版本中弃用。请使用 而是 AudioServicesPlaySystemSoundWithCompletion。

注意: 如果振动不起作用,检查振动是否在声音和触觉设置中启用

我们可以在 Xcode7.1中实现这一点

import UIKit
import AudioToolbox




class ViewController: UIViewController {


override func viewDidLoad() {
super.viewDidLoad()


AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
}
}

在 iPhone7或7 Plus 的 iOS10中,试试:

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

其他振动类型:

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)

AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))

对于 IOS 10.0 + ,你可以试试 反馈发生器

上面的简单 viewController,只需在测试“ 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
}
}
}

Swift 4.2更新

只需在项目中插入下面的代码。

用法

Vibration.success.vibrate()

源代码

  enum Vibration {
case error
case success
case warning
case light
case medium
case heavy
@available(iOS 13.0, *)
case soft
@available(iOS 13.0, *)
case rigid
case selection
case oldSchool


public func vibrate() {
switch self {
case .error:
UINotificationFeedbackGenerator().notificationOccurred(.error)
case .success:
UINotificationFeedbackGenerator().notificationOccurred(.success)
case .warning:
UINotificationFeedbackGenerator().notificationOccurred(.warning)
case .light:
UIImpactFeedbackGenerator(style: .light).impactOccurred()
case .medium:
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
case .heavy:
UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
case .soft:
if #available(iOS 13.0, *) {
UIImpactFeedbackGenerator(style: .soft).impactOccurred()
}
case .rigid:
if #available(iOS 13.0, *) {
UIImpactFeedbackGenerator(style: .rigid).impactOccurred()
}
case .selection:
UISelectionFeedbackGenerator().selectionChanged()
case .oldSchool:
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
}
}
}
import AudioToolbox


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

现在您可以根据需要调用 UIDevice.vibrate()

你可以使用 AudioServicesHaptic Feedback振动手机。

// AudioServices
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))


// Haptic Feedback
UIImpactFeedbackGenerator(style: .medium).impactOccurred()

看看我的开源框架 Haptica,它支持 Haptic FeedbackAudioServices和独特的振动模式

UINotificationFeedbackGenerator 可以从 iOS10获得,并且可以使用 Haptic v2,我们可以检查以下内容:

  let feedbackSupportLevel = UIDevice.current.value(forKey: "_feedbackSupportLevel") as? Int
if #available(iOS 10.0, *), let feedbackSupportLevel = feedbackSupportLevel, feedbackSupportLevel > 1 {
do { // 1
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
}


do { // or 2
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
}
} else {
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}

Swift 4.25.0

 if #available(iOS 10.0, *) {
UIImpactFeedbackGenerator(style: .light).impactOccurred()
}

您还可以选择其他样式,如

    style: .heavy
style: .medium


//Note: soft and rigid available in only iOS 13.0
style: .soft
style: .rigid