【问题标题】:How to make iPhone vibrate using Swift?如何使用 Swift 让 iPhone 振动?
【发布时间】:2014-10-19 22:00:42
【问题描述】:

我需要让 iPhone 振动,但我不知道如何在 Swift 中做到这一点。我知道在 Objective-C 中,你只需写:

import AudioToolbox
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

但这对我不起作用。

【问题讨论】:

标签: ios iphone swift vibration


【解决方案1】:

简短示例:

import UIKit
import AudioToolbox

class ViewController: UIViewController {

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

加载到您的手机上,它会振动。您可以根据需要将其放入函数或 IBAction 中。

代码更新:

 AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { }

正如苹果代码文档所写:

此功能将在未来版本中弃用。采用 改为 AudioServicesPlaySystemSoundWithCompletion。

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

【讨论】:

  • 有没有办法让手机一直振动直到按下按钮?
  • 尼古拉斯问了什么,我也想知道是否有办法让它以某种特殊的方式振动。
  • 我知道有一个 github 可以显示所有 iPhone 音频文件和 3 个振动文件的系统 ID,但有人知道我如何访问其他振动文件吗?例如,我想使用 Staccato 振动,但我不知道 ID,或者即使有 ID。
  • @Nicholas 如果我发现一个应用程序要求我按下按钮,否则它将永远振动,我将立即从我的设备中删除它。
  • 请记住,如果您的应用的音频会话配置了 AVAudioSessionCategoryPlayAndRecord 或 AVAudioSessionCategoryRecord 音频会话类别,则设备不会振动。这样可以确保振动不会干扰录音。
【解决方案2】:

在 iPhone 7 或 7 Plus 上的 iOS 10 中,尝试:

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

【讨论】:

  • 我们都使用 iOS 10,不是吗?它是系统的最新、最好和免费的版本。每个人都应该更新,没有妥协。
  • 在 iOS 10 之后,这只是一个适用于 iPhone 7 或更高版本的功能。在 iPhone 7 之前的设备上将被忽略。
  • @C6Silver 你说得对。它不适用于 iPhone 4s、5、5s、5c 和 6。我测试过这些设备。
【解决方案3】:

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 AVFoundation”以使用 .oldSchool 振动 :)
  • 除了 .oldSchool 其余案例不工作我在 iPhone 6 物理设备上测试有什么帮助吗?
  • 你应该解释FeedbackGenerator是触觉的,它是iPhone 7附带的。你也可以每个案例只有一行:UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
  • 唯一对我有用的是.oldSchool。我正在使用带有 iOS 14 的 iPhone 6s。iPhone 6s 确实有一个轻触引擎,它在切换静音开关时使用。是否因为苹果想让 iPhone 7 看起来更好而被禁用?或者,也许我只是做错了什么。 编辑:没关系,迈克尔的回答对我有用。
【解决方案4】:

其他振动类型:

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)

更多关于振动的信息 - http://www.mikitamanko.com/blog/2017/01/29/haptic-feedback-with-uifeedbackgenerator/

【讨论】:

  • 这似乎是在 iPhone 6s 上获得触觉反馈的唯一方法
【解决方案5】:

Swift 4.2、5.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

【讨论】:

    【解决方案6】:

    iOS 10.0+可以试试UIFeedbackGenerator

    上面的简单视图控制器,只需在您的测试“单视图应用程序”中替换您的视图控制器

    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
            }
        }
    }
    

    【讨论】:

      【解决方案7】:

      我们可以在 Xcode7.1 中做到这一点

      import UIKit
      import AudioToolbox
      
      
      class ViewController: UIViewController {
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
          }
      }
      

      【讨论】:

        【解决方案8】:

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

        // AudioServices
        AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
        
        // Haptic Feedback
        UIImpactFeedbackGenerator(style: .medium).impactOccurred()
        

        查看我的开源框架Haptica,它支持Haptic FeedbackAudioServices 和独特的振动模式。 适用于 Swift 4.2、Xcode 10

        【讨论】:

          【解决方案9】:
          import AudioToolbox
          
          extension UIDevice {
              static func vibrate() {
                  AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
              }
          }
          

          现在您可以根据需要拨打UIDevice.vibrate()

          【讨论】:

            【解决方案10】:

            iOS 10 提供 UINotificationFeedbackGenerator 并与 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))
                    }
            

            【讨论】:

              【解决方案11】:
              AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
              

              【讨论】:

              猜你喜欢
              • 2011-06-11
              • 2010-09-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-04-17
              相关资源
              最近更新 更多