iOS9 3D Touch 使用教程

本教程为纯代码,亦可以使用info.plist文件实现3D Touch功能

代码如下

通过self.traitCollection.forceTouchCapability方法可以判断当前设备是否支持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:@"您的手机支持3dtouch" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"很遗憾您的手机不支持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:@"[email protected]"];
    
     /** type 该item 唯一标识符   
      localizedTitle :标题  
      localizedSubtitle:副标题
      icon:icon图标 可以使用系统类型 也可以使用自定义的图片
      userInfo:用户信息字典 自定义参数,完成具体功能需求
      */
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"item1" localizedTitle:@"科目一" localizedSubtitle:@"参加科目一练习" icon:icon1 userInfo:nil];
    
//    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"[email protected]"];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"item2" localizedTitle:@"科目二" localizedSubtitle:@"参加科目二练习" icon:icon2 userInfo:nil];
    
    UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"[email protected]"];
    UIApplicationShortcutItem *item3= [[UIApplicationShortcutItem alloc] initWithType:@"item3" localizedTitle:@"科目三" localizedSubtitle:@"参加科目三练习" icon:icon3 userInfo:nil];
    
    UIApplicationShortcutIcon *icon4 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"[email protected]"];
    UIApplicationShortcutItem *item4 = [[UIApplicationShortcutItem alloc] initWithType:@"item4" localizedTitle:@"科目四" localizedSubtitle:@"参加科目四练习" icon:icon4 userInfo:nil];
    
     /** 将items 添加到app图标 */
    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"])
    {
         /** 具体操作 */
    }
}

系统icon图标枚举

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;

通过info.plist文件添加方法

iOS9 3D Touch 使用教程_第1张图片
Paste_Image.png

以下代码内容摘自官方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}

UIApplicationShortcutItems

    

        

            UIApplicationShortcutItemIconFile

            open-favorites

            UIApplicationShortcutItemTitle

            Favorites

            UIApplicationShortcutItemType

            com.mycompany.myapp.openfavorites

            UIApplicationShortcutItemUserInfo

            

                key1

                value1

            

        

        

            UIApplicationShortcutItemIconType

            UIApplicationShortcutIconTypeCompose

            UIApplicationShortcutItemTitle

            New Message

            UIApplicationShortcutItemType

            com.mycompany.myapp.newmessage

            UIApplicationShortcutItemUserInfo

            

                key2

                value2

            

        

    

以下内容为info.plist文件各个字段的解释

iOS9 3D Touch 使用教程_第2张图片
Paste_Image.png

至此,3D touch的基本使用已经完成,后期将继续更新预览功能等。。。

你可能感兴趣的:(iOS9 3D Touch 使用教程)