• iOS 振动反馈


    代码地址如下:
    http://www.demodashi.com/demo/12461.html

    1. 常用场景

    继 iPhone7/7P 实体 home 键出现后,home 键再也无法通过真实的物理按压反馈给用户了, apple 使用各种不同的振动效果代替了之前的按压反馈,效果非常好。
    振动反馈的使用场景还仅费如此,现在很多 app 中增加了振动反馈的效果。
    例:
    ① 新浪微博,下拉刷新会有振动效果反馈
    ② 摩拜单车,扫码开锁成功后会有振动效果

    2. 源码解释

    cocoa 框架中提供了几种振动反馈效果

    
    // OC 代码
    
    // UIImpactFeedbackGenerator
    typedef NS_ENUM(NSInteger, UIImpactFeedbackStyle) {
        UIImpactFeedbackStyleLight,
        UIImpactFeedbackStyleMedium,
        UIImpactFeedbackStyleHeavy
    };
    
    // UINotificationFeedbackGenerator
    typedef NS_ENUM(NSInteger, UINotificationFeedbackType) {
        UINotificationFeedbackTypeSuccess,
        UINotificationFeedbackTypeWarning,
        UINotificationFeedbackTypeError
    };
    
    
    
    /// Swift 代码
    
    public enum UIImpactFeedbackStyle : Int {
        case light
        case medium
        case heavy
    }
    
    public enum UINotificationFeedbackType : Int {
        case success
        case warning
        case error
    }
    
    

    UIImpactFeedbackGeneratorUINotificationFeedbackGenerator 均继承于 UIFeedbackGenerator

    封装

    OC 版

    // 创建一个枚举
    typedef enum {
        light = 0,
        medium,
        heavy,
        success,
        warning,
        error,
        none
    }FeedbackType;
    
    

    Swift 版本

    
    /// 创建枚举
    public enum FeedbackType: Int {
        case light
        case medium
        case heavy
        case success
        case warning
        case error
        case none
    }
    
    /// 创建类方法,随时调用
    class func impactFeedback(style: FeedbackType) {
    
            if #available(iOS 10.0, *) {
    
                switch style {
                case .light:
                    let generator = UIImpactFeedbackGenerator(style: .heavy)
                    generator.impactOccurred()
                case .medium:
                    let generator = UIImpactFeedbackGenerator(style: .medium)
                    generator.impactOccurred()
                case .heavy:
                    let generator = UIImpactFeedbackGenerator(style: .heavy)
                    generator.impactOccurred()
                case .success:
                    let generator = UINotificationFeedbackGenerator()
                    generator.notificationOccurred(.success)
                case .warning:
                    let generator = UINotificationFeedbackGenerator()
                    generator.notificationOccurred(.warning)
                case .error:
                    let generator = UINotificationFeedbackGenerator()
                    generator.notificationOccurred(.error)
                default:
                    break
                }
    
            }
    
        }
    
    

    3. 项目结构图

    对系统方式进行封装,更方便的调用
    代码分 OC 版和 Swift 版

    4. Demo 截图


    注意事项

    请使用真机测试效果。
    要求系统版本最低 iOS10.

    iOS 振动反馈

    代码地址如下:
    http://www.demodashi.com/demo/12461.html

    注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权

  • 相关阅读:
    TextEdit 使用Mask验证输入格式为邮箱
    大牛博客收藏
    WPF的Dispatcher类里的BeginInvoke,Invoke,InvokeAsync
    DispatcherPriority 枚举
    WPF 线程处理
    Winform UI线程和处理线程交互(进度更新显示)
    C# DataTable 类使用
    string.Format对C#字符串格式化
    C# 线程学习记录
    WPF 控件库
  • 原文地址:https://www.cnblogs.com/demodashi/p/8512751.html
Copyright © 2020-2023  润新知