iOS9 3DTouch 之 Home Screen Quick Actions

最后更新:2016-12-18

测试环境:

Xcode8.1

一、前言

iOS9 已经过去一年了,3D Touch也在项目中实战过,但一直没有总结一下,现在新的项目也用到了3D Touch, 网上找了找资料,很杂,打算自己总结一下,希望不妥之处,还望指正。


二、参考链接:

  1. 苹果的Getting Started with 3D Touch
  2. 苹果的UIApplicationShortcutItems 介绍
  1. 参考链接放前面,主要是 这个很简答,官方文档已经很详细了。可以直接去看也可以的;
  2. 推荐直接去看官方文档,英文不好也可以接着看下去。

三、理论来一波

  1. 苹果提供了两种快速启动方式,分别为 静态快速启动(static quick actions)动态快速启动(dynamic quick actions);

  2. 这两种的主要区别就在于:

    • 静态方式的所展示的样式不可改变;
      我们的产品-粉粉日记采用的就是静态方式,定义的内容不可以改变(固定的四种);
      QQ20161209-0.png-194.5kB

    • 动态方式,是可以根据内容自定义的;
      而天猫采用的就是动态的方式:

      • 定制样式一:
        image_1b3gk6tar18f5140cmdingstp222.png-213.6kB
      • 定制样式二:
        image_1b3gk9gva1tm01nv1vrk1h1hfu32f.png-208.4kB

四、静态快速启动

  1. 打开Xcode,新建一个项目,命名为 StaticQuickActionSimpleDemo
    image_1b3n177131ipb155p1db3ark15dv9.png-204.8kB
    image_1b3n1aa951v4a12ql1a3d16co10km.png-186.5kB
    image_1b3n1bmut9qq211m5i1p99bt913.png-128kB

  2. 在项目的Info.plist中添加以下键值对, 然后Cmd + R 运行起来,shift + cmd + H ,看看效果;

	<key>UIApplicationShortcutItems</key>
	<array>
		<dict>
			<key>UIApplicationShortcutItemIconFile</key>
			<string>shortcut_RemindWriteDiary</string>
			<key>UIApplicationShortcutItemTitle</key>
			<string>文字</string>
			<key>UIApplicationShortcutItemType</key>
			<string>RemindWriteDiary</string>
		</dict>
		<dict>
			<key>UIApplicationShortcutItemIconFile</key>
			<string>shortcut_RemindTakePhoto</string>
			<key>UIApplicationShortcutItemTitle</key>
			<string>拍照</string>
			<key>UIApplicationShortcutItemType</key>
			<string>RemindTakePhoto</string>
		</dict>
		<dict>
			<key>UIApplicationShortcutItemIconFile</key>
			<string>shortcut_QuickNote</string>
			<key>UIApplicationShortcutItemTitle</key>
			<string>便利贴</string>
			<key>UIApplicationShortcutItemType</key>
			<string>QuickNote</string>
		</dict>
		<dict>
			<key>UIApplicationShortcutItemIconFile</key>
			<string>shortcut_AccountsRecord</string>
			<key>UIApplicationShortcutItemTitle</key>
			<string>记账本</string>
			<key>UIApplicationShortcutItemType</key>
			<string>AccountsRecord</string>
		</dict>
	</array>

image_1b3n1tbt71ljn9iqkdds721ag11g.png-427.2kB
image_1b3n223ckpagtpuj431koomas1t.png-383.3kB
3. 貌似可以了,但是系统怎么相应呢?
需要在 Appdelegate中,我们需要添加一个代理方法来响应。

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler

image_1b3n2sdc1poegoa19l54e51bju2n.png-304.3kB


4. 键的解释说明:

说明
UIApplicationShortcutItemType (必要) 唯一的字符串,来表示快速启动的类型,根据此名称来表示,您启动的是哪个类别
UIApplicationShortcutItemTitle (必要) 展示在启动上面的标题名称,例如 粉粉日记 上面的 文字、类别、便利贴、记账本
UIApplicationShortcutItemSubtitle 可选值,子标题,类似于 UITableViewCell的子标题
UIApplicationShortcutItemIconType 可选值,官方定义的Icon,伴随着 标题名称一起展示
UIApplicationShortcutItemIconFile 可选值,可自定义的Icon,如果设置了此项, UIApplicationShortcutItemIconType将无效
UIApplicationShortcutItemUserInfo 可选值,可以传递一些信息进去,一般不怎么用

五、动态快速启动

  1. 新建一个项目,名字为 DynamicQucikActionSimpleDemo
    image_1b3n5u6cl1soi1mb1tnr1vd73nb3h.png-137.6kB
  2. 简单的在APPDelegate配置一个;

此处仅仅简单的配置,也可在其他地方配置

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"ShortcutItemWrite" localizedTitle:@"记一笔" localizedSubtitle:nil icon:icon1 userInfo:nil];
    
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay];
    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"ShortcutItemPlay" localizedTitle:@"播放" localizedSubtitle:nil icon:icon2 userInfo:nil];
    [UIApplication sharedApplication].shortcutItems = @[item1, item2];
    
    return YES;
}

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    if ([shortcutItem.type isEqualToString:@"ShortcutItemWrite"]) {
        NSLog(@"选择了记一笔");
    } else if ([shortcutItem.type isEqualToString:@"ShortcutItemPlay"]) {
        NSLog(@"播放");
    }
}
@end

运行结果:
image_1b3naijb51us6fla1t5c10ci8jq3u.png-482kB

点击后效果如下:
image_1b3nakctns4v37613eg1knck3a4o.png-31.3kB


posted @ 2016-12-18 21:46  洒水先生  阅读(115)  评论(0编辑  收藏  举报