美文网首页iOS学习笔记iOS Developer程序员
iOS 震动反馈(UIFeedbackGenerator)和系统

iOS 震动反馈(UIFeedbackGenerator)和系统

作者: whbalzac | 来源:发表于2017-09-15 00:14 被阅读0次

    震动反馈(UIFeedbackGenerator)

    闹钟滑动时间的震动反馈.png

    震动反馈是iOS 10之后出的新特性,相比于之前的系统震动
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
    要友好得多,没有声音,震动幅度适中,不需要设置里“响铃模式震动”打开。这也是Apple更推荐开发者使用的反馈震动。
    e.g. Switch控件滑动,时钟里选时间滑动,产生的震动都是UIFeedbackGenerator特性的。
    现在“震动反馈”的应用是非常广的 —— 下拉刷新;点击重要的Button;选择器等等。都可以加上反馈。
    Apple文档(UIFeedbackGenerator)

    //
    //  UIImpactFeedbackGenerator.h
    //  UIKit
    //
    //  Copyright © 2016 Apple Inc. All rights reserved.
    //
    
    #import <UIKit/UIFeedbackGenerator.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    typedef NS_ENUM(NSInteger, UIImpactFeedbackStyle) {
        UIImpactFeedbackStyleLight,
        UIImpactFeedbackStyleMedium,
        UIImpactFeedbackStyleHeavy
    };
    
    // UIImpactFeedbackGenerator is used to give user feedback when an impact between UI elements occurs
    UIKIT_CLASS_AVAILABLE_IOS_ONLY(10_0) @interface UIImpactFeedbackGenerator : UIFeedbackGenerator
    
    - (instancetype)initWithStyle:(UIImpactFeedbackStyle)style;
    
    /// call when your UI element impacts something else
    - (void)impactOccurred;
    
    @end
    

    想要用震动反馈也特别简单:

    UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
    [feedBackGenertor impactOccurred];
    
    注意:“UIImpactFeedbackGenerator' is only available on iOS 10.0 or newer”,使用的时候加上版本限制。**

    手机 -- 设置 -- 声音与触感 -- 系统触感反馈(打开)

    系统触感反馈.png

    此前系统震动AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)

    在iOS 10之前,系统震动采用的是震动+铃声的模式,目前看来是及其不友好的,首先震动略大,其次带声音,体验并不好。但这种的方式可以自定义音效。
    Apple文档(AudioServicesPlaySystemSound)

    #import <AudioToolbox/AudioToolbox.h>  
    
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    
    注意:手机 -- 设置 -- 声音与触感 -- 响铃模式震动(打开)
    响铃模式震动.png

    相关文章

      网友评论

        本文标题:iOS 震动反馈(UIFeedbackGenerator)和系统

        本文链接:https://www.haomeiwen.com/subject/xuvjsxtx.html