iOS 3D Touch简单使用
来源:互联网 发布:数控铣床编程实验报告 编辑:程序博客网 时间:2024/04/30 03:02
由于3d touch功能只在部分机型可以使用,因此在使用前需要判断手机是否支持该功能
//判断3d touch是否可用 if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) { [self addTouchItems]; }
3d touch的标签有2种设置方式,一种静态的,一种动态的,静态的需要直接在info.plist文件中添加字段,动态的则只有在程序运行过后才会产生.
1.静态添加
在info.plist文件中添加以下字段和内容
2.动态添加
在App的根视图添加以下代码
- (void)addTouchItems { UIApplicationShortcutIcon * icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay]; UIApplicationShortcutItem * item1 = [[UIApplicationShortcutItem alloc] initWithType:@"c" localizedTitle:@"vc3" localizedSubtitle:nil icon:icon1 userInfo:nil]; UIApplicationShortcutIcon * icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch]; UIApplicationShortcutItem * item2 = [[UIApplicationShortcutItem alloc] initWithType:@"d" localizedTitle:@"vc4" localizedSubtitle:nil icon:icon2 userInfo:nil]; //自定义icon图标 UIApplicationShortcutIcon * icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@""]; [UIApplication sharedApplication].shortcutItems = @[item1, item2];}
完成后效果图如下
当用户通过点击快速标签进入App时如何回应
在Appdelegate中有新的回调方法可以使用
//当点击快速标签进入app时调用- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler { if ([shortcutItem.type isEqualToString:@"a"]) { self.window.rootViewController.view.backgroundColor = [UIColor redColor]; } else { self.window.rootViewController.view.backgroundColor = [UIColor greenColor]; }}
peek预览视图
要实现peek预览,需要接收UIViewControllerPreviewingDelegate协议,实现协议方法,同时,要对需要实现预览的view进行注册
假设要对一个tableview中的每个cell进行预览,代码如下
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"mycell" forIndexPath:indexPath]; [self registerForPreviewingWithDelegate:self sourceView:cell]; //这一句对每个cell进行注册,可以实现按压每个cell实现预览 cell.textLabel.text = self.dataArr[indexPath.row]; return cell;}
注册完成后需要实现2个代理方法
//获取按压的cell,展示预览视图(peek)- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location { //根据按压位置获得按压视图// CGPoint p = [self.table convertPoint:location fromCoordinateSpace:self.view];// NSIndexPath * path = [self.table indexPathForRowAtPoint:p];// UITableViewCell * cell = [self.table cellForRowAtIndexPath:path]; //[previewingContext sourceView] 获取重压所在的view NSIndexPath * indexPath = [self.table indexPathForCell:(UITableViewCell *)[previewingContext sourceView]]; UITableViewCell * cell = [self.table cellForRowAtIndexPath:indexPath]; UIStoryboard * sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController * detail = [sb instantiateViewControllerWithIdentifier:NSStringFromClass([DetailViewController class])]; detail.preferredContentSize = CGSizeMake(0, self.view.frame.size.height); UILabel * lab = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 30)]; lab.text = cell.textLabel.text; [detail.view addSubview:lab]; return detail;}//进入预览视图(pop)- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit { //以这种方式进入详情控制器后可以返回预览视图 [self.navigationController pushViewController:viewControllerToCommit animated:YES]; //以这种方式进入的话无法返回 [self showDetailViewController:viewControllerToCommit sender:self];}
效果图
出现预览视图时,我们可以向上滑动,底部会出现几个选项,这些选项的配置需要在预览视图中配置,假设当前是在a控制器,预览视图是b控制器,那么需要在b中写
//设置peek后显示的视图向上滑动时底部显示的选项- (NSArray<id<UIPreviewActionItem>> *)previewActionItems { UIPreviewAction *p1 = [UIPreviewAction actionWithTitle:@"选项1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { //点击选项后触发的操作 NSLog(@"1111111"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"111111" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; }]; UIPreviewAction *p2 = [UIPreviewAction actionWithTitle:@"选项2" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"2222222222"); }]; UIPreviewAction *p3 = [UIPreviewAction actionWithTitle:@"选项3" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"3333333333"); }]; return @[p1,p2,p3];}
最后继续重压cell,即可pop进入b控制器,并且如果采用push方式,还可以返回a
0 0
- iOS 3D Touch简单使用
- iOS开发 3D Touch的简单使用
- iOS开发 3D-touch使用
- IOS 3D touch的使用
- ios 3D Touch 的使用
- iOS 9 以后使用3D touch()
- ios 3D Touch
- iOS 3D touch
- iOS 3D Touch
- iOS 3D Touch
- IOS 实现3D Touch在tableView的简单应用
- android 简单模仿IOS的3D Touch功能
- iOS 3D Touch使用<二、 Peek、Pop使用>
- iOS 3D Touch开发
- iOS 3D Touch功能
- iOS 3D touch 开发
- iOS 3D Touch功能
- iOS 3D Touch 简述
- wordpress仿雷锋网2016最新主题下载
- ServletContextListener使用详解
- 响铃:诸侯之争,美泊凭什么占领代泊市场高地?
- LinuxApache源码安装经验
- 史上最全设计模式导学目录(完整版)
- iOS 3D Touch简单使用
- Oracle移植到mysql
- Matlab中读取txt文件的几种方法
- 上传
- 虎嗅网WordPress模板 2016最新版
- if else
- mysql主从同步总结
- 大前端XIU5.2版本主题xiu最新版
- 我的第2次C++实验