3D Touch,苹果iPhone 6s的新功能,看起来类似 PC 上的右键。有Peek Pop 两种新手势,2015年9月10日,苹果在新品发布会上宣布了3D-Touch功能。
Peek和Pop
现在你可以授权应用的视图控制器来响应用户不同的按压力量。随着用户按压力量的增加,交互会出现三个阶段:
1. 暗示内容预览是可使用的
2. 展示预览(peek),和快捷选项菜单(peek quick actions)
3. 可选的跳转到预览中的视图(pop)
当你使用 peek 和 pop 时,系统通过压力决定从哪个阶段过度至下一个。用户可以在设置>通用>辅助功能>3D Touch中进行修改。
暗示peek是可使用的
轻按后,周围内容会变得模糊,这告诉用户预览更多内容( peek )是可以使用的。
Peek
轻按,屏幕视图就会过渡到 peek,一个你设置的用来展示更多内容的视图-就像Mail app做的一样。如果用户这时结束了触碰,peek就会消失并且应用回到交互开始之前的状态。
代码段如下(最低在 xcode7.0版本以上):
在 AppDelegate.m 中
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
//判断先前我们设置的唯一标示符
if ([shortcutItem.type isEqualToString:@"-11.UITouchText.share"]) {
NSArray *arr = @[@"hello 3D Touch"];
UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:arr applicationActivities:nil];
[self.window.rootViewController presentViewController:vc animated:YES completion:^{
}];
}
if ([shortcutItem.type isEqualToString:@"-11.UITouchText.Search"]) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
SearchViewController *search = [storyboard instantiateViewControllerWithIdentifier:@"searchID"];
UINavigationController *nac = [[UINavigationController alloc] initWithRootViewController:search];
[self.window.rootViewController presentViewController:nac animated:YES completion:^{
}];
}
if ([shortcutItem.type isEqualToString:@"-11.UITouchText.play"]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
picker.allowsEditing = YES;//设置可编辑
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.window.rootViewController presentViewController:picker animated:YES completion:nil];//进入图片库
}
}
//在Viewcontroller写一个长按手势用于区分长按和3dtouch 的区别
_longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction)];
#pragma mark 检验3Dtouch
- (void)check3Dtouch{
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[self registerForPreviewingWithDelegate:self sourceView:self.view];
NSLog(@"3D touch 开启状态");
_longPress.enabled = NO;
return;
}
_longPress.enabled = YES;
}
#pragma mark 预览代理方法
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
_temp = (location.y / 100) - 1;
PreviewController *previewC = [[PreviewController alloc] init];
previewC.view.frame = self.view.frame;
UIImageView *er = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_array[_temp]]];
previewC.view = er;
return previewC;
}
#pragma mark 上拉代理方法
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
UIPreviewAction *p1 =[UIPreviewAction actionWithTitle:@"分享" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享了" message:@"你好" delegate: self cancelButtonTitle:@"嗯嗯" otherButtonTitles:@"说的对啊", nil];
[alert show];
}];
UIPreviewAction *p2 =[UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收藏了" message:@"你好" delegate: self cancelButtonTitle:@"艾玛" otherButtonTitles:@"呵呵", nil];
[alert show];
}];
NSArray *actions = @[p1,p2];
return actions;
}