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:@"item1@3x.png"];
    
     /** type 该item 唯一标识符   
      localizedTitle :标题  
      localizedSubtitle:副标题
      icon:icon图标 可以使用系统类型 也可以使用自定义的图片
      userInfo:用户信息字典 自定义参数,完成具体功能需求
      */
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"item1" localizedTitle:@"科目一" localizedSubtitle:@"参加科目一练习" icon:icon1 userInfo:nil];
    
//    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"item2@3x.png"];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"item2" localizedTitle:@"科目二" localizedSubtitle:@"参加科目二练习" icon:icon2 userInfo:nil];
    
    UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"item3@3x.png"];
    UIApplicationShortcutItem *item3= [[UIApplicationShortcutItem alloc] initWithType:@"item3" localizedTitle:@"科目三" localizedSubtitle:@"参加科目三练习" icon:icon3 userInfo:nil];
    
    UIApplicationShortcutIcon *icon4 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"item4@3x.png"];
    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文件添加方法

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}

<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>

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

Paste_Image.png

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 155,809评论 24 680
  • 专著:http://www.jianshu.com/p/3443a3b27b2d 1.简单的介绍一下3D Touc...
    violafa阅读 598评论 1 0
  • 前言 关于这篇文章 由于iPhone 6S发布不到一年的时间,很多新特性、新技术还未普遍,不管是3D Touch的...
    Tangentw阅读 3,063评论 8 18
  • 来也一场,去也一场。 又是一个雨天,行去亦匆匆。 无奈困于小巷处,自在呻吟。
    初心练习曲阅读 61评论 0 2
  • 从开始跑步到现在,已经是第四个年头了。不是严格意义上的每天都跑,总会有各种各样的原因导致短则几天长则几个月的间断,...
    微跑阅读 103评论 1 1