好久沒有更新簡書了,真是很不好意思。未來一個月可能還是不能及時更新,還請見諒。
今天因為時間有限,給大家介紹一個挺有用,而且十分簡單的功能:3DTouch。
3DTouch.jpeg
如圖,大家在使用iPhone6S以上機型的時候一定用到過這個功能。而且它的實現原理很簡單。這里給大家介紹兩種:
1.代碼實現:
-(void)setup3DTouch {
/// 這里的字符串的是圖片名稱,就是顯示在3DTouch上的圖片
UIApplicationShortcutIcon *itemIconCart = [UIApplicationShortcutIcon iconWithTemplateImageName:@"cart"];
/// 這里的中文名是名稱。相對應的@“cart”起的是標識作用,相當于tag,來告訴我們用戶點擊的按鈕
UIApplicationShortcutItem *itemCart = [[UIApplicationShortcutItem alloc] initWithType:@"cart" localizedTitle:@"購物車" localizedSubtitle:@"" icon:itemIconCart userInfo:nil];
UIApplicationShortcutIcon *itemIconDesigner = [UIApplicationShortcutIcon iconWithTemplateImageName:@"designer"];
UIApplicationShortcutItem *itemDesigner = [[UIApplicationShortcutItem alloc] initWithType:@"designer" localizedTitle:@"品牌" localizedSubtitle:@"" icon:itemIconDesigner userInfo:nil];
UIApplicationShortcutIcon *itemIconSearch = [UIApplicationShortcutIcon iconWithTemplateImageName:@"search"];
UIApplicationShortcutItem *itemSearch = [[UIApplicationShortcutItem alloc] initWithType:@"search" localizedTitle:@"搜索" localizedSubtitle:@"" icon:itemIconSearch userInfo:nil];
NSMutableArray *arrShortcutItem = (NSMutableArray *)[UIApplication sharedApplication].shortcutItems;
[arrShortcutItem addObjectsFromArray:@[itemCart, itemDesigner, itemSearch]];
/// 將需要的3DTouch按鈕放入集合之中
[UIApplication sharedApplication].shortcutItems = arrShortcutItem;
}
很簡單,最終的效果就是第一張圖片。
2.plist完成:
plist.jpeg
UIApplicationShortcutItemIconFile
3DTouch按鈕的圖片名稱
UIApplicationShortcutItemTitle
按鈕顯示名稱
UIApplicationShortcutItemType
唯一標識符,就是相當于tag的作用
UIApplicationShortcutItemSubtitle
快捷可選項的子標題
UIApplicationShortcutItemIconType
快捷可選項的圖標
UIApplicationShortcutItemUserInfo
快捷可選項的附加信息
好了,介紹完如何顯示,接下來我們再介紹用戶點擊之后我們需要怎么操作。其實簡單,在AppDelegate中,有一個方法:
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
所有的操作只需要在這個方法中實現就可以了
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
NSLog(@"name:%@\ntype:%@", shortcutItem.localizedTitle, shortcutItem.type);
}
接下來,我點擊“搜索”,看看會有什么打印出來。
搜索.jpeg
所以大家只需要在方法之中將對應的邏輯完成就可以了。很簡單吧。
喜歡的朋友可以收藏一下哈。??