自己写的好用的3D Touch封装类

作者: 许佩利翁 | 来源:发表于2017-04-30 21:48 被阅读130次

    最近工作不忙,无聊的封装了3D Touch工具,使用简单,来跟大家分享一下,喜欢的朋友请加星哦😄。
    CXForceTouchManager github地址

    触发的样式图
    下面附加使用方法
    /** 检测ForceTouch是否可用 **/
    + (BOOL)checkForceTouchCapabilityAvailable;
    
    /** 开启设置方法 **/
    + (void)makeShortcutItems:(void(^_Nonnull)(CXForceTouchManager *_Nonnull manager))block;
    
    /** 添加item的方法
        type 通知标识符
        title 标题
        subtitle 副标题
        imageName 图片名
     */
    - (CXForceTouchManager *_Nonnull(^_Nonnull)(NSString * _Nonnull type, NSString * _Nonnull title, NSString * _Nullable subtitle, NSString * _Nonnull imageName))addItem;
    
    /** 完成添加item的方法 **/
    - (CXForceTouchManager * _Nonnull (^_Nonnull)())finished;
    
    /** 点击item的方法 **/
    + (void)didSelectItemWithType:(NSString *_Nonnull)type;
    
    
    在项目中的实际使用如下
    1. 需要添加3D Touch快捷按钮
    if([CXForceTouchManager checkForceTouchCapabilityAvailable]) {
            [CXForceTouchManager makeShortcutItems:^(CXForceTouchManager *manager) {
                manager.addItem(@"kNotificationForceTouch1",@"按键一",nil,@"add");
                manager.addItem(@"kNotificationForceTouch2",@"按键二",nil,@"addTime");
                manager.addItem(@"kNotificationForceTouch3",@"按键三",nil,@"clock");
                manager.finished();
            }];
        } else {
            NSLog(@"Force Touch 不可用");
        }
    
    1. 在AppDelegate里- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler方法中调用3D Touch按钮被点击事件发出通知
    [CXForceTouchManager didSelectItemWithType:shortcutItem.type];
    
    1. 通过type类型(如:kNotificationForceTouch1)添加通知的接收者
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kNotificationForceTouch1Action) name:@"kNotificationForceTouch1" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kNotificationForceTouch2Action) name:@"kNotificationForceTouch2" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kNotificationForceTouch3Action) name:@"kNotificationForceTouch3" object:nil];
    
    1. 通知调用的跳转方法如下:
     - (void)kNotificationForceTouch1Action {
        FirstViewController *first = [FirstViewController new];
        [self.window.rootViewController presentViewController:first animated:YES completion:nil];
    }
     - (void)kNotificationForceTouch2Action {
        SecondViewController *second = [SecondViewController new];
        [self.window.rootViewController presentViewController:second animated:YES completion:nil];
    }
     - (void)kNotificationForceTouch3Action {
        ThirdViewController *third = [ThirdViewController new];
        [self.window.rootViewController presentViewController:third animated:YES completion:nil];
    }
    
    1. 记得要在dealloc方法中移除通知哦
      - (void)dealloc {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kNotificationForceTouch1" object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kNotificationForceTouch2" object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kNotificationForceTouch3" object:nil];
    }
    

    相关文章

      网友评论

        本文标题:自己写的好用的3D Touch封装类

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