如何让iPhone使用Swift振动?

问题描述 投票:97回答:11

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

import AudioToolbox
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

但那不适合我。

ios iphone swift vibrate
11个回答
205
投票

简短的例子:

import UIKit
import AudioToolbox

class ViewController: UIViewController {

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

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


0
投票
import AudioToolbox

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

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


0
投票

在这里,您将找到每个.caf和相关类别的所有代码:

https://github.com/TUNER88/iOSSystemSoundsLibrary

例如,如果您想要更轻的振动,可以使用代码1003。

祝好运并玩得开心点 ;)


57
投票

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

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

43
投票

其他振动类型:

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/


23
投票

Swift 4.2更新

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

用法

Vibration.success.vibrate()

源代码

import AVFoundation
import UIKit

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

    }

}

21
投票

对于iOS 10.0+,您可以尝试UIFeedbackGenerator

上面简单的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
        }
    }
}

16
投票

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

import UIKit
import AudioToolbox


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
    }
}

5
投票

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

// AudioServices
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))

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

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


1
投票
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))

1
投票

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))
        }
© www.soinside.com 2019 - 2024. All rights reserved.