IOS 实现3D Touch在tableView的简单应用
3D Touch正在改变用户的操作习惯,体验了下,确实挺酷。 做为个开发者,一定想知道如何才能集成这样操作功能。
话不多说,直接上代码(因为3D Touch的开发资料网止已经太多了,不说了)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif)
一、3D Touch重按主屏icon出现快捷标签有两种添加方式:
info.plist
文件中添加,这种方式添加的标签安装后就能显示
![](/assets/blank.gif)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[selfconfigShortCutItems];
//很多资料上查到要判断,但发现我加上之后通过标签启动时不会加载预览界面
// if (launchOptions[@"UIApplicationLaunchOptionsShortcutItemKey"] == nil)
// {
return YES;
// }
// else
// {
// return NO;
// }
}
- (void)configShortCutItems
{
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIconiconWithType:UIApplicationShortcutIconTypePlay];
UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItemalloc]initWithType:@"888"localizedTitle:@"play"localizedSubtitle:@"动态添加的标签"icon:icon1userInfo:nil];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIconiconWithTemplateImageName:@"shareIcon"];
UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItemalloc]initWithType:@"999"localizedTitle:@"自定义图标"localizedSubtitle:@"呵呵"icon:icon2userInfo:nil];
[UIApplicationsharedApplication].shortcutItems =@[item1,item2];
}
需要说明一点,主屏呼出的快捷标签,静态和动态添加的共只能有四个。
3D Touch在appdelegate.m代理方法:
#pragma mark - 3D Touch
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler{
//判断设置的唯一标识,区分点击的是哪一个标签来响应不同事件
if([shortcutItem.typeisEqualToString:@"666"]){
DDDTouchVC *vc = [[DDDTouchVC alloc] init];
McUINavigationController *nav_VC = [[McUINavigationController alloc] initWithRootViewController:vc];
[nav_VC setBackgroundImage:[UIImage imageNamed:@"nav_bg"]];
[self.window.rootViewController presentViewController:nav_VC animated:YES completion:^{
}];
return;
}
if([shortcutItem.typeisEqualToString:@"777"]){
DDDTouchVC *vc = [[DDDTouchVC alloc] init];
McUINavigationController *nav_VC = [[McUINavigationController alloc] initWithRootViewController:vc];
[nav_VC setBackgroundImage:[UIImage imageNamed:@"nav_bg"]];
[self.window.rootViewController presentViewController:nav_VC animated:YES completion:^{
}];
return;
}
if ([shortcutItem.typeisEqualToString:@"888"]) {
DDDTouchVC *vc = [[DDDTouchVC alloc] init];
McUINavigationController *nav_VC = [[McUINavigationController alloc] initWithRootViewController:vc];
[nav_VC setBackgroundImage:[UIImage imageNamed:@"nav_bg"]];
[self.window.rootViewController presentViewController:nav_VC animated:YES completion:^{
}];
return;
}
if ([shortcutItem.typeisEqualToString:@"999"]) {
NSArray *arr = @[@"3D Touch"];
UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:arr applicationActivities:nil];
[self.window.rootViewController presentViewController:vc animated:YES completion:^{
}];
return;
}
}
至此,通过重按主屏触发3D Touch 呼出快捷标签和点击分别响应操作已集成完毕!
二、3D Touch 的peek和pop功能在tableView上的简单实现
话不多说,直接上代码
1.peek预览
DDDTouchVC.h
#import <UIKit/UIKit.h>
@interface DDDTouchVC :UIViewController
@end
这是主控制器,有tableView,也是3D Touch的代理控制器需求实现 UIViewControllerPreviewingDelegate 协议
DDDTouchVC.m
#import "DDDTouchVC.h"
#import "DDDTouchPeekVC.h"
@interface DDDTouchVC ()<UIViewControllerPreviewingDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)NSMutableArray *arrData;
@property (nonatomic,retain)UITableView *tableView;
@implementation DDDTouchVC
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.title =@"3D Touch";
UIButton *back = [UIButtonbuttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0,0,40,25);
[back setTitle:@"exit" forState:UIControlStateNormal];
[back setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[back setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[back addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:back];
[self.navigationItem setLeftBarButtonItem:leftItemanimated:YES];
[selfinitTableView];
//检测是否支持3D Touch...
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
//支持
// [self registerForPreviewingWithDelegate:self sourceView:self.view];
}
else
{
//不支持
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"手机不支持3D Touch..." message:nil delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil,nil];
[alert show];
}
}
- (void)initTableView
{
//百度随便找了些图片链接
NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"http://a.hiphotos.baidu.com/image/h%3D360/sign=1698b41ebb389b5027ffe654b535e5f1/a686c9177f3e6709e974ebff39c79f3df8dc55bd.jpg" forKey:@"1"];
NSDictionary *dic2 = [NSDictionary dictionaryWithObject:@"http://b.hiphotos.baidu.com/image/h%3D360/sign=ff34dff8bd389b5027ffe654b537e5f1/a686c9177f3e670900d880193fc79f3df9dc5578.jpg" forKey:@"2"];
NSDictionary *dic3 = [NSDictionary dictionaryWithObject:@"http://a.hiphotos.baidu.com/image/h%3D360/sign=f527ab2f4b540923b5696578a259d1dc/dcc451da81cb39dbce6b325ad2160924ab183016.jpg" forKey:@"3"];
NSDictionary *dic4 = [NSDictionary dictionaryWithObject:@"http://d.hiphotos.baidu.com/image/h%3D360/sign=81d5d22779f40ad10ae4c1e5672d1151/d439b6003af33a8762fee505c45c10385343b51a.jpg" forKey:@"4"];
NSDictionary *dic5 = [NSDictionary dictionaryWithObject:@"http://d.hiphotos.baidu.com/image/h%3D200/sign=8d3a52a04ded2e73e3e9812cb701a16d/f7246b600c33874450b89258560fd9f9d72aa091.jpg" forKey:@"5"];
NSDictionary *dic6 = [NSDictionary dictionaryWithObject:@"http://h.hiphotos.baidu.com/image/h%3D360/sign=e53dee25d5ca7bcb627bc1298e086b3f/a2cc7cd98d1001e9ae32c3e9ba0e7bec55e797ce.jpg" forKey:@"6"];
NSDictionary *dic7 = [NSDictionary dictionaryWithObject:@"http://g.hiphotos.baidu.com/image/h%3D360/sign=8cb0e6191a178a82d13c79a6c602737f/6c224f4a20a446230761b9b79c22720e0df3d7bf.jpg" forKey:@"7"];
NSDictionary *dic8 = [NSDictionary dictionaryWithObject:@"http://a.hiphotos.baidu.com/image/h%3D360/sign=80285822f9f2b211fb2e8348fa806511/bd315c6034a85edf1da1c0724b540923dd5475b5.jpg" forKey:@"8"];
NSDictionary *dic9 = [NSDictionarydictionaryWithObject:@"http://e.hiphotos.baidu.com/image/h%3D360/sign=708629f379cb0a469a228d3f5b62f63e/7dd98d1001e939015d4a463779ec54e736d1966b.jpg"forKey:@"9"];
NSDictionary *dic10 = [NSDictionary dictionaryWithObject:@"http://h.hiphotos.baidu.com/image/h%3D360/sign=95102c30bc096b639e1958563c338733/3801213fb80e7beccbb1e8fe2d2eb9389b506b42.jpg" forKey:@"10"];
NSDictionary *dic11 = [NSDictionary dictionaryWithObject:@"http://a.hiphotos.baidu.com/image/h%3D360/sign=cbd227bf8e5494ee9822091f1df5e0e1/c2cec3fdfc039245ee84470f8594a4c27d1e25f8.jpg" forKey:@"11"];
NSDictionary *dic12 = [NSDictionary dictionaryWithObject:@"http://e.hiphotos.baidu.com/image/h%3D360/sign=87824d652adda3cc14e4be2631e83905/b03533fa828ba61e64671cd54534970a314e59bb.jpg" forKey:@"12"];
_arrData = [[NSMutableArray alloc] initWithObjects:dic1,dic2,dic3,dic4,dic5,dic6,dic7,dic8,dic9,dic10,dic11,dic12,nil];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-64)style:UITableViewStylePlain];
_tableView.dataSource =self;
_tableView.delegate =self;
[self.viewaddSubview:_tableView];
}
#pragma mark - 3D Touch Peek和pop代理
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
//对每个cell进行注册代理方法:[self registerForPreviewingWithDelegate:self sourceView:cell];
//通过[previewingContext sourceView]拿到tableView对应的cell
NSIndexPath *indexPath = [_tableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
NSString *imageURL = [[[_arrData objectAtIndex:indexPath.row] allValues] objectAtIndex:0];
DDDTouchPeekVC *peekVC = [[DDDTouchPeekVC alloc] initWithImageURL:imageURL];
peekVC.view.backgroundColor = [UIColor greenColor];
NSLog(@"%@",location);
return peekVC;
}
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
viewControllerToCommit.hidesBottomBarWhenPushed =YES;
[self showViewController:viewControllerToCommit sender:self];
// [self.navigationController pushViewController:viewControllerToCommit animated:NO];
}
#pragma mark - UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (_arrData &&_arrData.count >0) {
return _arrData.count;
}
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID =@"LMChao";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellID];
//注册Peek高亮来源***
[self registerForPreviewingWithDelegate:self sourceView:cell];
}
if (_arrData && indexPath.row < [_arrDatacount])
{
cell.textLabel.text = [[[_arrData objectAtIndex:indexPath.row] allKeys] objectAtIndex:0];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//do somthing...
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
2.pop界面控制器
DDDTouchPeekVC.h
#import <UIKit/UIKit.h>
@interface DDDTouchPeekVC : UIViewController
@property (nonatomic,retain)NSString *imageURL;
- (id)initWithImageURL:(NSString *)imageURL;
@end
DDDTouchPeekVC.m
#import "DDDTouchPeekVC.h"
@interface DDDTouchPeekVC ()
{
UIImageView *_photoView;
}
@property (nonatomic,copy)NSString *etag;
@end
@implementation DDDTouchPeekVC
- (id)initWithImageURL:(NSString *)imageURL
{
if (self = [superinit]) {
//do somthing..
_imageURL = imageURL;
}
return self;
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *back = [UIButtonbuttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0,0,40,25);
[back setTitle:@"back" forState:UIControlStateNormal];
[back setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[back setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[back addTarget:selfaction:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:back];
[self.navigationItem setLeftBarButtonItem:leftItem animated:YES];
self.title =@"3D Touch PeekVC";
_photoView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.view.frame),CGRectGetHeight(self.view.frame)-64)];
[self.viewaddSubview:_photoView];
[self initData];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:YES];
}
- (void)backButtonClicked
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)initData
{
NSURL *url = [NSURLURLWithString:_imageURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0timeoutInterval:10.0];
if (self.etag.length >0) {
[request setValue:self.etag forHTTPHeaderField:@"IF-None-Match"];
}
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *_Nullable data,NSURLResponse *_Nullable response,NSError *_Nullable error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSLog(@"%@",httpResponse);
if (error.code ==0) {
dispatch_async(dispatch_get_main_queue(), ^{
//显示图片
_photoView.image = [UIImage imageWithData:data];
});
}
self.etag = httpResponse.allHeaderFields[@"etag"];
NSLog(@"%@",httpResponse.allHeaderFields);
}];
[task resume];
}
#pragma mark - 底部预览界面选项(peek界面时上滑弹起的快捷标签)
- (NSArray <id <UIPreviewActionItem>> *)previewActionItems
{
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"呵呵"style:UIPreviewActionStyleDefault handler:^(UIPreviewAction *_Nonnull action, UIViewController *_Nonnull previewViewController) {
NSLog(@"%s",__func__);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"呵呵" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
[alert show];
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"嘿嘿"style:UIPreviewActionStyleDefault handler:^(UIPreviewAction *_Nonnull action, UIViewController *_Nonnull previewViewController) {
NSLog(@"%s",__func__);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"嘿嘿" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
[alert show];
}];
UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"擦擦"style:UIPreviewActionStyleDefault handler:^(UIPreviewAction *_Nonnull action,UIViewController *_Nonnull previewViewController) {
NSLog(@"%s",__func__);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"擦擦..." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
[alert show];
}];
//想要显示多个就定义多个 UIPreviewAction
NSArray *actions = @[action1,action2,action3];
return actions;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 以下两个为默认设定,直接复制粘贴就行(网上看到网友有这样写的,好像可有可无)
// 根视图的返回
- (UIViewController*)topViewController
{
return [self topViewControllerWithRootViewController:[UIApplicationsharedApplication].delegate.window.rootViewController];
}
// 根视图的类型
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController
{
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabBarController = (UITabBarController *)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewControllerisKindOfClass:[UINavigationControllerclass]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
至此,3D Touch功能在tableView上列表的简单功能就完成了, 这边是peek 、pop展示的是加载一张百度连接的图片。匆忙而就,写得不详细,如有错误,谅解。。。
IOS 实现3D Touch在tableView的简单应用相关推荐
- IOS 实现3D Touch在tableView的简单应用(swift版)
之前记录过OC版实现3D Touch功能的小小演示,最近无事整整swift,也弄个swift版的3D Touoch... 上代码. 一.3D Touch重按主屏icon出现快捷标签有两种添加方式: 1 ...
- iOS.用3D touch长按图标来实现快捷操作
自苹果在2015年发布3dtouch功能以来, iPhone 6s之后的机型全都匹配了 3dtouch功能.此功能的发布将手机屏幕的的操作坐标由xy轴扩大至z轴,增加了整整一个维度,这在屏幕时代属于非 ...
- 苹果的新技术3D Touch与以往的“长按屏幕”有何区别?
本质区别: 长按 = (x, y) + 时间: 3D Touch = (x, y, z): 举个例子:题主想象手握一支沾了墨的毛笔, 1.你用笔尖轻轻在纸上一点,会出现一个点: 2.你同样的力度把笔尖 ...
- Android 7.1.1 之实现 3D Touch
转载请注明出处:http://blog.csdn.net/yyh352091626/article/details/68962736 Shortcut概念 具体实现 BuildConfig 配置 静态 ...
- 美团推出外卖版拼多多;iOS 14 Beta 3暂禁用3D Touch功能;Rust 1.45 发布| 极客头条
整理 | 屠敏 头图 | CSDN 下载自东方 IC 扫描二维码,即刻收听 极客头条音频版 智能播报由出门问问「魔音工坊」提供技术支持 「极客头条」-- 技术人员的新闻圈! CSDN 的读者朋友们早上 ...
- ios touch坐标_iOS 3D Touch –窥视与流行
ios touch坐标 In this tutorial, we'll be discussing and implementing the 3D Touch functionality in our ...
- iOS开发之3D Touch(快速添加3D Touch功能)
1. 概述 在支持3D Touch的设备上,用户可以通过对触摸屏施加不同程度的压力来访问其他功能,应用程序可以通过显示上下文菜单(或支持Peek和Pop)来响应,以显示一些可供用户操作的选项或者行为. ...
- IOS 3D Touch 开发教程
前一阵子伴随着苹果发布新产品iPhone6s 和 IOS9新系统,一系列关于硬件和软件的新功能.新特性逐渐进入iPhone用户的视线.这次我们就来讲讲传闻已久的3D Touch. 当然这个新功能不仅仅 ...
- iOS 3D Touch浅谈
一.什么是3D Touch? 3D Touch是iPhone 6s推出的一种可以让你与手机进行互动的全新方式.除了轻点.轻扫.双指开合这些熟悉的 Multi‑Touch 手势之外,3D Touch 还 ...
最新文章
- python源代码的后缀名是_Python代码编译与反编译
- php去小城市找还是大城市,想去大城市工作?这是冲动还是理性的选择呢?
- Luogu2114 [NOI2014]起床困难综合症 【位运算】
- 使用Dozer框架进行Bean操作
- arcengine,深入理解游标Cursors,实现数据的快速查找,插入,删除,更新
- invokedynamic指令
- python plot 增加标记线_Python可视化| matplotlib04-掌握标记和线型的使用,一文,marker,linestyle...
- mysql死锁案例及解决方案_MySQL死锁案例分析与解决方案
- 10月8日提交的结对编程工程中的经验和教训
- 更改eclipse字体
- HTML Layui Distpicker 住址/收货地址选择 联动选择框
- 《雍正皇帝》文化专有词翻译策略的研究现状(Baker)
- REST ful风格
- myeclipse的server视图经常报nullpoint
- 大众点评api接口 开放平台
- 福建农林大学计算机与信息学院宿舍,2021福建农林大学宿舍条件和新生宿舍图片及分配规则分享...
- ARM处理器的工作模式。
- ES集群搭建错误~Too many open files
- wifi 联想小新_WiFi变了,联想小新打印机简单设置教程
- WinForm隐藏窗口控制最大化最小化按钮
热门文章
- 幽默及顿悟的哲理故事
- 财政年度末(fiscal year-end)
- 申宝证券-指数呈现缩量大涨的走势
- Android源码编译 ViVo Xplay2.17.3 最新官方固件/自动语音相机/固件root/官方系统优化/支持OTA/稳定省电ROM
- [置顶]第二弹:升级原创日语汉字转假名小工具1.02
- 北斗三号频点_全球首颗全面支持北斗三号基带芯片问世
- 2022年最新中国科学院期刊分区表变化 | 生物类、医学类
- 细解“微服务”架构体系——SpringCloud Alibaba!
- html 组件化 编辑器,谈谈前端组件化
- UDP 发送 MediaPlayer播放