iPhone用力长按触发3D-Touch效果,如下:
实现3D-Touch效果方法有两种:
一、在.plist文件中添加 UIApplicationShortcutItems (不太推荐)
方法a:
直接添加 UIApplicationShortcutItems 项;
再添加item项:(需要的属性)
UIApplicationShortcutItemIconType、
UIApplicationShortcutItemTitle、
UIApplicationShortcutItemType;
这样就已经可以用力长按触发3d-touch了。

方法b:
右键.plist文件,open as Source Code

添加以下代码:
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeShare</string>
<key>UIApplicationShortcutItemTitle</key>
<string>分享</string>
<key>UIApplicationShortcutItemType</key>
<string>3dTouchDemo.openShare</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key2</key>
<string>value2</string>
</dict>
</dict>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeSearch</string>
<key>UIApplicationShortcutItemTitle</key>
<string>搜索</string>
<key>UIApplicationShortcutItemType</key>
<string>3dTouchDemo.openSearch</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key1</key>
<string>value1</string>
</dict>
</dict>
</array>
二、在AppDelegate中添加 UIApplicationShortcutItems (推荐,维护起来比较方便)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSMutableArray *shortcutItemsArray = (NSMutableArray *)[UIApplication sharedApplication].shortcutItems;
UIApplicationShortcutItem *shortItem1 = [[UIApplicationShortcutItem alloc] initWithType:@"3dTouchDemo.openMail" localizedTitle:@"联系我们" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeMail] userInfo:nil];
[shortcutItemsArray addObject:shortItem1];
UIApplicationShortcutItem *shortItem2 = [[UIApplicationShortcutItem alloc] initWithType:@"3dTouchDemo.openLove" localizedTitle:@"收藏" localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLove] userInfo:nil];
[shortcutItemsArray addObject:shortItem2];
[UIApplication sharedApplication].shortcutItems = shortcutItemsArray;
return YES;
}
// 点击触发此方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
NSLog(@"%@",shortcutItem.localizedTitle);
NSLog(@"%@",shortcutItem.type);
}
网友评论