如何让iPhone使用Swift振动?

时间:2014-10-19 22:00:42

标签: ios iphone swift vibrate

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

import AudioToolbox
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

但那对我不起作用。

12 个答案:

答案 0 :(得分:200)

简短的例子:

import UIKit
import AudioToolbox

class ViewController: UIViewController {

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

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

答案 1 :(得分:52)

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

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

答案 2 :(得分:41)

其他振动类型:

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/

答案 3 :(得分: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
        }
    }
}

答案 4 :(得分:18)

Swift 4.2更新

只需将下面的代码插入您的项目即可。

用法

iteration <- c(1,2,3,4,5,6)
calc <- c(1,4,7,9,4,6)
optimal <- c(6,8,3,6,7,6)

plot(iteration,calc,type="p",col="red")
points(iteration,optimal,col="green")

源代码

library(ggplot2)
ggplot(data, aes(x=iteration, y=calc))+
geom_point(aes(color="red"))+
geom_point(aes(y=optimal, color="green"))

答案 5 :(得分:16)

我们可以在Xcode7.1中执行此操作

import UIKit
import AudioToolbox


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
    }
}

答案 6 :(得分:4)

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

// AudioServices
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))

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

签出我的开源框架Haptica,它同时支持Haptic FeedbackAudioServices和独特的振动模式。 在Swift 4.2,Xcode 10上运行

答案 7 :(得分:2)

Swift 4.2

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

答案 8 :(得分:1)

AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))

答案 9 :(得分:1)

可从iOS 10获得并与Haptic v2一起使用的UINotificationFeedbackGenerator,我们可以进行以下检查:

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

答案 10 :(得分:0)

import AudioToolbox

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

现在,您可以根据需要致电UIDevice.vibrate()

答案 11 :(得分:0)

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

https://github.com/TUNER88/iOSSystemSoundsLibrary

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

祝你好运,玩得开心;)