如何让iPhone使用Swift振动?

Nic*_*ich 109 iphone vibration ios swift

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

import AudioToolbox
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
Run Code Online (Sandbox Code Playgroud)

但那不适合我.

Ste*_*erg 211

简短的例子:

import UIKit
import AudioToolbox

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))            
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 尼古拉斯问的是什么,我也想知道是否有办法让它以某种特殊的方式振动. (3认同)
  • 请记住,如果您的应用的音频会话配置为 AVAudioSessionCategoryPlayAndRecord 或 AVAudioSessionCategoryRecord 音频会话类别,则设备不会振动。这可确保振动不会干扰录音。 (2认同)

Ada*_*aka 62

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

let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
Run Code Online (Sandbox Code Playgroud)

  • 在iOS 10之外,这只是一项适用于iPhone 7或更高版本的功能.在早于iPhone 7的设备上将被忽略. (12认同)

Mic*_*ael 46

其他振动类型:

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)
Run Code Online (Sandbox Code Playgroud)

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


muh*_*urk 27

Swift 4.2更新

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

用法

Vibration.success.vibrate()
Run Code Online (Sandbox Code Playgroud)

源代码

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

    }

}
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记添加'import AVFoundation'以便使用.oldSchool振动:) (3认同)

Mal*_*der 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


ken*_*nek 17

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

import UIKit
import AudioToolbox


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这仍然适用于Swift 4,Xcode 9. (4认同)

Ras*_*tif 7

斯威夫特4.2

 if #available(iOS 10.0, *) {
      UIImpactFeedbackGenerator(style: .light).impactOccurred()
   } 
Run Code Online (Sandbox Code Playgroud)


efr*_*dze 5

您可以使用AudioServices或使手机振动Haptic Feedback。

// AudioServices
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))

// Haptic Feedback
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
Run Code Online (Sandbox Code Playgroud)

结帐我的开源框架Haptica,它同时支持Haptic Feedback,AudioServices以及独特的振动模式。适用于 Swift 4.2、Xcode 10