[iOS] UIFeedbackGenerator example (iOS 햅틱반응 API)

iPhone 7의 홈 물리버튼이 사라지면서, iOS 10 때 추가된 API 입니다.

Developer Apple - UIFeedbackGenerator (링크 참고)

샘플소스는 매우 간단한 편이며, 예외처리 필요없이 햅틱기능이 있는 기기에서만 작동 합니다.

Github : UIFeedbackGenerator-Sample

ViewController.swift


import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }    
    /// UIButton Touch Up Inside Action
    ///
    /// - Parameter sender: UIButton (tag)
    @IBAction func doFeedbackGenerator(_ sender: Any) {
        let senderButton:UIButton = sender as! UIButton
        switch senderButton.tag {
        case 1:
            let generator = UINotificationFeedbackGenerator()
            generator.notificationOccurred(.success)
        case 2:
            let generator = UINotificationFeedbackGenerator()
            generator.notificationOccurred(.warning)            
        case 3:
            let generator = UINotificationFeedbackGenerator()
            generator.notificationOccurred(.error)
        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()
        }
    }
}
cs

댓글

  1. /// call when your UI element impacts something else with a specific intensity [0.0, 1.0]
    - (void)impactOccurredWithIntensity:(CGFloat)intensity API_AVAILABLE(ios(13.0));

    [참고] iOS 13 이상부터는 진동 값 조절 할 수 있는것 같습니다.

    답글삭제

댓글 쓰기