iOS 3D Touch

释放双眼,带上耳机,听听看~!

硬件支持 iPhone 6s以上

系统支持iOS9以上

  • 实现方式有两种:一种是plist文件(静态),一种是代码(动态)。
    静态添加方式优先动态添加方式。
    从手机桌面重按icon弹出的选项最多五个,其中程序写入最多能显示四个,另一个是app上架后系统自带的分享功能

如果要屏蔽掉通过代码添加的选项,再查看效果,要删除app,重新载入。

静态方式:

首先要添加一个UIApplicationShortcutItems的数组,在这个数组下添加键值对。

必填项:

UIApplicationShortcutItemType 这个键值设置一个快捷通道类型的字符串
UIApplicationShortcutItemTitle 这个键值设置标签的标题

选填项:

UIApplicationShortcutItemSubtitle 设置标签的副标题
UIApplicationShortcutItemIconType 设置标签Icon类型
UIApplicationShortcutItemIconFile 设置标签的Icon文件
UIApplicationShortcutItemUserInfo 设置信息字典(用于传值)

使用系统icon的UIApplicationShortcutItemIconType枚举

 

typedef NS_ENUM(NSInteger, UIApplicationShortcutIconType) {
    UIApplicationShortcutIconTypeCompose,
    UIApplicationShortcutIconTypePlay,
    UIApplicationShortcutIconTypePause,
    UIApplicationShortcutIconTypeAdd,
    UIApplicationShortcutIconTypeLocation,
    UIApplicationShortcutIconTypeSearch,
    UIApplicationShortcutIconTypeShare,
    UIApplicationShortcutIconTypeProhibit       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeContact        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeHome           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMarkLocation   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeFavorite       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeLove           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCloud          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeInvitation     NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeConfirmation   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMail           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMessage        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeDate           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTime           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCapturePhoto   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCaptureVideo   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTask           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTaskCompleted  NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeAlarm          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeBookmark       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeShuffle        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeAudio          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeUpdate         NS_ENUM_AVAILABLE_IOS(9_1)
} NS_ENUM_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;

动态方式:

在AppDelegate.m文件的

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

添加如下方法:

 

[self creatUIApplicationShortcutItemsWithOptions:launchOptions];

 

-(void)creatUIApplicationShortcutItemsWithOptions:(NSDictionary *)options{
    
    /*
     通过代码调用系统icon的方法如下:
     UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLocation];
     */
    
    //自定义的icon,即UIApplicationShortcutItemIconFile类型
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"money"];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"gouwuche"];
    UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"wode"];
    
    //创建UIApplicationShortcutItemUserInfo
    NSDictionary *info1 = @{@"url":@"money"};
    NSDictionary *info2 = @{@"url":@"gouwuche"};
    NSDictionary *info3 = @{@"url":@"wode"};
    
    //创建UIMutableApplicationShortcutItem
    UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"money"
                                                                                     localizedTitle:@"钱包"
                                                                                  localizedSubtitle:@"这是钱包"
                                                                                               icon:icon1
                                                                                           userInfo:info1];
    
    UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"gouwuche"
                                                                                     localizedTitle:@"购物车"
                                                                                  localizedSubtitle:@"这是购物车"
                                                                                               icon:icon2
                                                                                           userInfo:info2];
    
    UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"wode"
                                                                                     localizedTitle:@"我的"
                                                                                  localizedSubtitle:@"这是我的"
                                                                                               icon:icon3
                                                                                           userInfo:info3];
    
    //添加item
    [UIApplication sharedApplication].shortcutItems = @[item1, item2, item3];
    
}

到这就结束了吗?你touch一下选项试试呗!

是不是发现问题了?在控制台打印了一串

 

Could not load IOSurface for time string. Rendering locally instead.

对溜,还没结束呢……

不管是通过静态方式还是动态方式添加的touch选项,都会调用一个共同的代理方法,如下:

 

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler;

上面的代码复制黏贴完了之后,别忘了要把这个代理方法也黏贴到AppDelegate.m里面,任意位置都行,只要你喜欢,只要程序不报错,只要程序不报警告!

 

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler{
    
    //嘟 腰 萨 母 sing
    NSLog(@"shortcutItem = %@",shortcutItem.type);
    NSLog(@"shortcutItemUrl = %@",shortcutItem.userInfo);

    if (completionHandler){
        completionHandler(YES);
    }

}

ok,这回彻底结束了

如果想了解peek和pop功能的请看这里

 

人已赞赏
iOS文章

iOS KVO监听WebView内容高度变化,同步父控件高度

2019-12-24 2:12:11

iOS文章

iOS 设置View的单个边的Border

2019-12-24 12:08:29

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索