How to make iPhone vibrate using Swift?

后端 未结 11 1538
天命终不由人
天命终不由人 2020-11-29 16:08

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:

import AudioToolbox
AudioServicesPl         


        
相关标签:
11条回答
  • 2020-11-29 16:23

    Swift 4.2, 5.0

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

    You can also choose other styles like

        style: .heavy
        style: .medium
    
        //Note: soft and rigid available in only iOS 13.0
        style: .soft
        style: .rigid
    
    0 讨论(0)
  • 2020-11-29 16:26

    For iOS 10.0+ You can try UIFeedbackGenerator

    Simple viewController above, just replace your view controller in your test "single view app"

    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
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:27

    We can do this in Xcode7.1

    import UIKit
    import AudioToolbox
    
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:32

    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.

    Code Update:

     AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { }
    

    As apple code docs written:

    This function will be deprecated in a future release. Use AudioServicesPlaySystemSoundWithCompletion instead.

    NOTE: If vibrate doesn't work. check vibrate is enable in sounds and haptics settings

    0 讨论(0)
  • 2020-11-29 16:37

    You can vibrate the phone using either AudioServices or 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

    0 讨论(0)
提交回复
热议问题