iPhone用力長按觸發3D-Touch效果,如下:
IMG_0377.PNG
實現3D-Touch效果方法有兩種:
一、在.plist文件中添加 UIApplicationShortcutItems (不太推薦)
方法a:
直接添加 UIApplicationShortcutItems 項;
再添加item項:(需要的屬性)
UIApplicationShortcutItemIconType、
UIApplicationShortcutItemTitle、
UIApplicationShortcutItemType;
這樣就已經可以用力長按觸發3d-touch了。
image.png
方法b:
右鍵.plist文件,open as Source Code
image.png
添加以下代碼:
<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);
}