本教程為純代碼,亦可以使用info.plist文件實(shí)現(xiàn)3D Touch功能
代碼如下
通過(guò)self.traitCollection.forceTouchCapability方法可以判斷當(dāng)前設(shè)備是否支持3d touch
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #00afca}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures; color: #c2349b}span.s2 {font-variant-ligatures: no-common-ligatures; color: #ffffff}span.s3 {font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #e44448}span.s5 {font: 14.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: #e44448}span.s6 {font-variant-ligatures: no-common-ligatures; color: #00afca}
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您的手機(jī)支持3dtouch" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"很遺憾您的手機(jī)不支持3dtouch" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ffffff}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4cbf57}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ffffff; min-height: 19.0px}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #00afca}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'PingFang SC'; color: #4cbf57}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #c2349b}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #d28f5a}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #c2349b}span.s3 {font-variant-ligatures: no-common-ligatures; color: #00afca}span.s4 {font-variant-ligatures: no-common-ligatures; color: #ffffff}span.s5 {font: 14.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}span.s6 {font-variant-ligatures: no-common-ligatures; color: #e44448}span.s7 {font: 14.0px Monaco; font-variant-ligatures: no-common-ligatures}span.s8 {font: 14.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: #e44448}span.s9 {font-variant-ligatures: no-common-ligatures; color: #8b84cf}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/** 定義 shortcutItem */
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"item1@3x.png"];
/** type 該item 唯一標(biāo)識(shí)符
localizedTitle :標(biāo)題
localizedSubtitle:副標(biāo)題
icon:icon圖標(biāo) 可以使用系統(tǒng)類型 也可以使用自定義的圖片
userInfo:用戶信息字典 自定義參數(shù),完成具體功能需求
*/
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"item1" localizedTitle:@"科目一" localizedSubtitle:@"參加科目一練習(xí)" icon:icon1 userInfo:nil];
// UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"item2@3x.png"];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"item2" localizedTitle:@"科目二" localizedSubtitle:@"參加科目二練習(xí)" icon:icon2 userInfo:nil];
UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"item3@3x.png"];
UIApplicationShortcutItem *item3= [[UIApplicationShortcutItem alloc] initWithType:@"item3" localizedTitle:@"科目三" localizedSubtitle:@"參加科目三練習(xí)" icon:icon3 userInfo:nil];
UIApplicationShortcutIcon *icon4 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"item4@3x.png"];
UIApplicationShortcutItem *item4 = [[UIApplicationShortcutItem alloc] initWithType:@"item4" localizedTitle:@"科目四" localizedSubtitle:@"參加科目四練習(xí)" icon:icon4 userInfo:nil];
/** 將items 添加到app圖標(biāo) */
application.shortcutItems = @[item4,item3,item2,item1];
return YES;
}
#pragma mark - 3D touch 代理方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler
{
/** 邏輯判斷 */
if ([shortcutItem.type isEqualToString:@"item1"])
{
/** 具體操作 */
}
}
系統(tǒng)icon圖標(biāo)枚舉
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ffffff}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #d28f5a}span.s1 {font-variant-ligatures: no-common-ligatures; color: #c2349b}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #d28f5a}span.s4 {font-variant-ligatures: no-common-ligatures; color: #8b84cf}span.s5 {font-variant-ligatures: no-common-ligatures; color: #ffffff}
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;
通過(guò)info.plist文件添加方法

Paste_Image.png
以下代碼內(nèi)容摘自官方API
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Courier; color: #666666; -webkit-text-stroke: #666666}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Courier; color: #666666; -webkit-text-stroke: #666666; min-height: 14.0px}span.s1 {font-kerning: none}table.t1 {border-collapse: collapse}td.td1 {width: 1239.0px; padding: 6.0px 6.0px 6.0px 6.0px}
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemIconFile</key>
<string>open-favorites</string>
<key>UIApplicationShortcutItemTitle</key>
<string>Favorites</string>
<key>UIApplicationShortcutItemType</key>
<string>com.mycompany.myapp.openfavorites</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key1</key>
<string>value1</string>
</dict>
</dict>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeCompose</string>
<key>UIApplicationShortcutItemTitle</key>
<string>New Message</string>
<key>UIApplicationShortcutItemType</key>
<string>com.mycompany.myapp.newmessage</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key2</key>
<string>value2</string>
</dict>
</dict>
</array>
以下內(nèi)容為info.plist文件各個(gè)字段的解釋

Paste_Image.png
至此,3D touch的基本使用已經(jīng)完成,后期將繼續(xù)更新預(yù)覽功能等。。。