ios 如何使用 Swift 让 iPhone 振动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26455880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make iPhone vibrate using Swift?
提问by Nico Gutraich
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:
我需要让 iPhone 振动,但我不知道如何在 Swift 中做到这一点。我知道在 Objective-C 中,你只需写:
import AudioToolbox
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
But that is not working for me.
但这对我不起作用。
回答by Steve Rosenberg
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.
加载到您的手机上,它会振动。您可以根据需要将其放入函数或 IBAction 中。
Code Update:
代码更新:
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { }
As apple code docs written:
正如苹果代码文档所写:
This function will be deprecated in a future release. Use AudioServicesPlaySystemSoundWithCompletion instead.
此功能将在未来版本中弃用。请改用 AudioServicesPlaySystemSoundWithCompletion。
NOTE:If vibrate doesn't work. check vibrate is enable in sounds and haptics settings
注意:如果振动不起作用。检查振动是否在声音和触觉设置中启用
回答by Adam Smaka
In iOS 10 on iPhone 7 or 7 Plus, try:
在 iPhone 7 或 7 Plus 上的 iOS 10 中,尝试:
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
回答by Michael
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/
更多关于振动 - http://www.mikitamanko.com/blog/2017/01/29/haptic-feedback-with-uifeedbackgenerator/
回答by muhasturk
Swift 4.2 Updated
斯威夫特 4.2 更新
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
@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))
}
}
}
回答by Malder
For iOS 10.0+You can try UIFeedbackGenerator
对于iOS 10.0+你可以试试UIFeedbackGenerator
Simple viewController above, just replace your view controller in your test "single view app"
上面的简单 viewController,只需在您的测试“单视图应用程序”中替换您的视图控制器
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
}
}
}
回答by ken0nek
We can do this in Xcode7.1
我们可以在 Xcode7.1 中做到这一点
import UIKit
import AudioToolbox
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
}
}
回答by Rashid Latif
Swift 4.2, 5.0
斯威夫特 4.2、5.0
if #available(iOS 10.0, *) {
UIImpactFeedbackGenerator(style: .light).impactOccurred()
}
回答by efremidze
You can vibrate the phone using either AudioServices
or Haptic Feedback
.
您可以使用AudioServices
或使手机振动Haptic Feedback
。
// AudioServices
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
// Haptic Feedback
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
Checkout my open source framework Haptica, it supports both Haptic Feedback
, AudioServices
and unique vibrations patterns. Works on Swift 4.2, Xcode 10
结帐我的开源框架Haptica,它同时支持Haptic Feedback
,AudioServices
以及独特的振动模式。适用于 Swift 4.2、Xcode 10
回答by Sergei Lysiuk
UINotificationFeedbackGenerator available from iOS 10 and work with Haptic v2, we can check this:
UINotificationFeedbackGenerator 可从 iOS 10 使用并与 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))
}
回答by Mohammad Razipour
import AudioToolbox
extension UIDevice {
static func vibrate() {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
}
}
Now you can just call UIDevice.vibrate()
as needed.
现在您可以UIDevice.vibrate()
根据需要调用。