文章目录
  1. 1. Home Screen Quick Actions
    1. 1.1. 静态标签
    2. 1.2. 动态标签
  2. 2. peep/pop
  3. 3. Force Properties

iphone6s的一个显著卖点应该是3D Touch,其原理是增加了一个压力感触,通过区分轻按和重按来进行不同的用户交互。

我们可以通过3D手势,在主屏幕上的应用Icon处,直接进入应用的响应功能模块。用户可以通过3D Touch手势在view上来预览一些预加载信息,这样的设计可以使app更加简洁大方,交互性也更强,也是对app的一个优化。

Home Screen Quick Actions

通过主屏幕的应用Icon,我们可以用3D Touch呼出一个菜单,进行快速定位应用功能模块相关功能的开发。

静态标签

静态标签是我们在项目的配置plist文件中配置的标签,在用户安装程序后就可以使用,并且排序会在动态标签的前面。

在Info.plist文件中添加UIApplicationShortcutItems数组,在改数组下建立一个包括UIApplicationShortcutItemTitle、UIApplicationShortcutSubtitle、UIApplicationShortcutItemIconFile、UIApplicationShortcutType键值对的字典即可,系统并没有提示,只能手打上去。

动态标签

动态标签是我们在程序中,通过代码添加的,与之相关的类,主要有三个:UIApplicationShortcutItem、UIMutableApplicationShortcutItem、UIApplicationShortcutIcon。

在appDelegate的-application:didFinishLaunchingWithOptions:方法中添加如下代码:

1.图标
UIApplicationShortcutIcon *icon1=[UIApplicationShortcutIcon iconWithTemplateImageName:@”iCon1”];

系统自带图标:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;

2.创建shortcut item
UIMutableApplicationShortcutItem *item1=[[UIMutableApplicationShortcutItem alloc] initWithType:@”com.zhuli8.dynamic” localizedTitle:@”Dynamic Shortcut” localizedSubtitle:@”available after first lauch” icon:icon1 userInfo:nil];

3.获取已经存在的shortcut item,例如静态的shortcut item
NSArray *existingItems=[UIApplication sharedApplication].shortcutItems;

4.当前所有的shortcut item赋值给当前应用程序
NSArray *updateItems=[existingItems arrayByAddingObjectsFromArray:@[item1]];
[UIApplication sharedApplication].shortcutItems=updateItems;

到此动态创建3d touch已经基本完成。如果要响应标签的行为,需要实现appDelegate中的一个方法-application:willFinishLaunchingWithOptions:

1
2
3
4
5
6
7
8
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{

if ([shortcutItem.type isEqualToString:@"com.zhuli8.deep1"]) {
ZLSecondeViewController *secondVC=[[ZLSecondeViewController alloc] init];
self.window.rootViewController=secondVC;
[self.window makeKeyAndVisible];
}
}

peep/pop

这个功能是一套全新的用户交互机制,在使用3D Touch时,ViewController中会有如下三个交互阶段:

  1. 提示用户这里有3D Touch的交互,会使交互控件周围模糊
  2. 继续深按,会出现预览视图
  3. 通过视图上的交互控件进行进一步交互

在控制器的-viewWillAppear:方法中判断设备是否有3d touch特性,根据返回值决定是否设置代理。

1
2
3
if (self.traitCollection.forceTouchCapability==UIForceTouchCapabilityAvailable) {
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}

实现UIViewControllerPreviewingDelegate的两个代理方法。

1
2
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0);
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0);

具体实例代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
//check if we're not already displaying a preview controller
if ([self.presentedViewController isKindOfClass:[ZLPreviewViewController class]]) {
return nil;
}

ZLPreviewViewController *previewVC=[[ZLPreviewViewController alloc] init];
return previewVC;
}

- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
//deep press:bring up the commit view controller(pop)
ZLCommitViewController *commitVC=[[ZLCommitViewController alloc] init];
[self showViewController:commitVC sender:self];
}

重写父类的previewActionItems方法配置preview菜单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
UIPreviewAction *action1=[UIPreviewAction actionWithTitle:@"action 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
ZLLogDebug(@"UIPreviewActionStyleDefault trggered");
}];

UIPreviewAction *action2=[UIPreviewAction actionWithTitle:@"destructive action" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
ZLLogDebug(@"UIPreviewActionStyleDestructive trggered");
}];

UIPreviewAction *action3=[UIPreviewAction actionWithTitle:@"selected action" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
ZLLogDebug(@"UIPreviewActionStyleSelected trggered");
}];

UIPreviewActionGroup *group1=[UIPreviewActionGroup actionGroupWithTitle:@"action group" style:UIPreviewActionStyleDefault actions:@[action1,action2,action3]];//可以返回组
return @[action1,action2,action3];
}

Force Properties

iOS9为我们提供了一个新的交互参数:力度。我们可以检测某一交互的力度值,来做相应的交互处理。例如,我们可以通过力度来控制快进的快慢,音量增加的快慢等。

文章目录
  1. 1. Home Screen Quick Actions
    1. 1.1. 静态标签
    2. 1.2. 动态标签
  2. 2. peep/pop
  3. 3. Force Properties