序言
3D Touch是一种立体触控技术,被苹果称为新一代多点触控技术,是在Apple Watch上采用的Force Touch,屏幕可感应不同的感压力度触控。3D Touch,苹果iPhone 6s的新功能,看起来类似 PC 上的右键。有Peek Pop 两种新手 [1] 势.
如何使用
用力按一个图标会弹出一层半透明菜单,里面包含了该应用下的一些快捷操作,在邮件列表上用力按也可以快速弹开窗口查看邮件,在邮件列表当中,用力按邮件列表里的一条邮件就会显示完整邮件内容,松手就会关掉,无需左右滑动的打开关闭操作。轻点电话就可以查看最近联系人,按压相机可以快速自拍,按压图片库可以快速浏览大图 [2]
编码实现
1. 静态设置
静态设置就是在项目的Info.plist文件中设置
代码如下
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeSearch</string>
<key>UIApplicationShortcutItemType</key>
<string>search</string>
<key>UIApplicationShortcutItemTitle</key>
<string>search</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>SubTitle1</string>
</dict>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeLove</string>
<key>UIApplicationShortcutItemType</key>
<string>wishlist</string>
<key>UIApplicationShortcutItemTitle</key>
<string>wishlist</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>SubTitle2</string>
</dict>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeBookmark</string>
<key>UIApplicationShortcutItemType</key>
<string>order</string>
<key>UIApplicationShortcutItemTitle</key>
<string>order</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>SubTitle3</string>
</dict>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeDate</string>
<key>UIApplicationShortcutItemType</key>
<string>what shot</string>
<key>UIApplicationShortcutItemTitle</key>
<string>whatshot</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>SubTitle3</string>
</dict>
</array>
效果如下
其中有几个可以设置的键值
//必须设置值的键
UIApplicationShortcutItemType //位置标识符
UIApplicationShortcutItemTitle //标题
//可选设置值得键
UIApplicationShortcutItemIconType //显示图标系统类型
UIAPPlicationShortcutItemIconFile //显示图标的图片名
UIAPPlicationShortcutItemUserInfo //用户信息字典信息,自定义参数
其中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;
2.动态代码实现
1.先建一个工具类,用于实现该功能
SysMenuHandler.h 文件
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
/**
3D_Touch 管理类
*/
@interface SysMenuHandler : NSObject
// 初始化菜单
+ (void)setShortcutMenus;
// 点击响应跳转
+ (void)open:(UIApplicationShortcutItem *)shortcutItem;
@end
SysMenuHandler.m 文件
#import "SysMenuHandler.h"
@implementation SysMenuHandler
// 初始化菜单
+ (void)setShortcutMenus {
NSMutableArray *itemMenus = [NSMutableArray array];
// search
UIApplicationShortcutItem *searchItem = [[UIApplicationShortcutItem alloc] initWithType:@"search"
localizedTitle:@"Search"
localizedSubtitle:nil
icon:[UIApplicationShortcutIcon iconWithType:
UIApplicationShortcutIconTypeSearch]
userInfo:nil];
[itemMenus addObject:searchItem];
// wish list
UIApplicationShortcutItem *wishItem = [[UIApplicationShortcutItem alloc] initWithType:@"wishlist"
localizedTitle:@"wish list"
localizedSubtitle:nil
icon:[UIApplicationShortcutIcon iconWithType:
UIApplicationShortcutIconTypeLove]
userInfo:nil];
[itemMenus addObject:wishItem];
// order
UIApplicationShortcutItem *orderItem = [[UIApplicationShortcutItem alloc] initWithType:@"order"
localizedTitle:@"menu order"
localizedSubtitle:nil
icon:[UIApplicationShortcutIcon iconWithType:
UIApplicationShortcutIconTypeBookmark]
userInfo:nil];
[itemMenus addObject:orderItem];
// what shot
UIApplicationShortcutItem *whatsHotItem = [[UIApplicationShortcutItem alloc] initWithType:@"whatshot"
localizedTitle:@"whatshot"
localizedSubtitle:nil
icon:[UIApplicationShortcutIcon iconWithType:
UIApplicationShortcutIconTypeDate]
userInfo:nil];
[itemMenus addObject:whatsHotItem];
// 响应到 APP 端
[UIApplication sharedApplication].shortcutItems = itemMenus;
}
// 点击响应跳转
+(void)open:(UIApplicationShortcutItem *)shortcutItem {
if ([shortcutItem.type isEqualToString:@"wishlist"]) {
NSLog(@"wishlist");
} else if ([shortcutItem.type isEqualToString:@"search"]) {
NSLog(@"search");
} else if ([shortcutItem.type isEqualToString:@"search"]) {
NSLog(@"search");
} else {
NSLog(@"wishlist");
}
}
代码的实现需要在方法-applocation:didFinishLaunchingWithOptions:中实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
#ifdef __IPHONE_9_0
[SysMenuHandler setShortcutMenus];
#endif
return YES;
}
响应方法
按下后如何响应
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem
completionHandler:(void (^)(BOOL))completionHandler {
#ifdef __IPHONE_9_0
[SysMenuHandler open:shortcutItem];
#endif
}
项目连接地址
生活源于创造,技术源于积累,写的不好的或者有错的地方,期望指正.