3D Touch簡(jiǎn)介
2015年,蘋果發(fā)布了iOS9以及iphone6s/iphone6s Plus,其中最具有創(chuàng)新的就是新的觸控方式3D Touch,相對(duì)于多點(diǎn)觸摸在平面二維空間的操作,3D Touch技術(shù)增加了對(duì)力度和手指面積的感知,可以通過(guò)長(zhǎng)按快速預(yù)覽、查看你想要的短信、圖片或者超鏈接等內(nèi)容,Peek和Pop手勢(shì)的響應(yīng)時(shí)間可迅捷到 10ms和15ms等。
3D Touch三大模塊
1. Home Screen Quick Actions
2. Peek、Pop
3. Force Properties
3D Touch實(shí)現(xiàn)
3D Touch實(shí)現(xiàn)起來(lái)不算難,就是實(shí)現(xiàn)需要硬件的支持,只能在6s/6s p等上面可以測(cè)試體驗(yàn),模擬器是不能的, ShortcutItem主要由Item類型,主標(biāo)題,副標(biāo)題、圖標(biāo),還可添加一些附加信息,每個(gè)App最多添加4個(gè)快捷鍵。
操作 - 用手指用力按壓App的icon,會(huì)出現(xiàn)類似的菜單快捷鍵(ShortcutItem),附上Demo的效果圖(Home Screen Quick Actions):
按壓icon.png
1. Home Screen Quick Actions
生成菜單 - 按壓icon彈出快捷鍵的實(shí)現(xiàn)方法分為靜態(tài)菜單、動(dòng)態(tài)菜單等2種。
. 靜態(tài)菜單 - 配置項(xiàng)目的.plist文件
目前Xcode版本的plist文件還沒(méi)對(duì)應(yīng)的key來(lái)獲取,則 需要用戶自己來(lái)手動(dòng)輸入U(xiǎn)IApplicationShortcutItems(NSArray類型 - 內(nèi)部都是字典- 對(duì)應(yīng) - 每個(gè)item);字典內(nèi)一些key的解釋:
UIApplicationShrtcutItemSubtitle (副標(biāo)題)
UIApplicationShrtcutItemTitle( 必填)(可監(jiān)聽(tīng)該項(xiàng)判斷用戶是從哪一個(gè)標(biāo)簽進(jìn)入App)
UIApplicationShortcutItemType( 必填)(可監(jiān)聽(tīng)該項(xiàng)判斷用戶是從哪一個(gè)標(biāo)簽進(jìn)入App)
UIApplicationShrtcutItemIconType(圖標(biāo))(系統(tǒng)提供了29種樣式的圖標(biāo))
UIApplicationShrtcutItemIconFile(自定義圖片的文件路徑)- 直接傳入圖片的名字即可
注意:若是設(shè)置了自定義的圖片 則系統(tǒng)的不再生效
.plist配置.png
. 動(dòng)態(tài)菜單 - 代碼實(shí)現(xiàn)快捷菜單,動(dòng)態(tài)添加方法需要代碼執(zhí)行一次,因此靜態(tài)方法比動(dòng)態(tài)方法優(yōu)先加載
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
? ?/**
? ? * ?通過(guò)代碼實(shí)現(xiàn)動(dòng)態(tài)菜單
? ? * ?一般情況下設(shè)置主標(biāo)題、圖標(biāo)、type等,副標(biāo)題是不設(shè)置的 - 簡(jiǎn)約原則
? ? * ?iconWithTemplateImageName 自定義的icon
? ? * ?iconWithType 系統(tǒng)的icon
? ? */
? ?//系統(tǒng)ShortcutIcon
? ?UIApplicationShortcutIcon *favrite = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeFavorite];
? ?UIApplicationShortcutItem *itemOne = [[UIApplicationShortcutItem alloc] initWithType:@"favrite" localizedTitle:@"時(shí)尚之都" localizedSubtitle:nil icon:favrite userInfo:nil];
? ?UIApplicationShortcutIcon *bookMark = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeBookmark];
? ?UIApplicationShortcutItem *itemTwo = [[UIApplicationShortcutItem alloc] initWithType:@"book" localizedTitle:@"知識(shí)海洋" localizedSubtitle:nil icon:bookMark userInfo:nil];
? ?//自定義ShortcutIcon
? ?UIApplicationShortcutIcon *iconContact = [UIApplicationShortcutIcon iconWithTemplateImageName:@"contact"];
? ?UIApplicationShortcutItem *itemThree = [[UIApplicationShortcutItem alloc] initWithType:@"contact" localizedTitle:@"聯(lián)系的人" localizedSubtitle:nil icon:iconContact userInfo:nil];
? ?[UIApplication sharedApplication].shortcutItems = @[itemOne,itemTwo,itemThree];
? ?return YES;
}
實(shí)現(xiàn)點(diǎn)擊菜單ShortcutItem對(duì)應(yīng)的item跳轉(zhuǎn)到對(duì)應(yīng)的頁(yè)面
//菜單跳轉(zhuǎn)
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
? ?UITabBarController *tabBarVC = (UITabBarController *)self.window.rootViewController;
? ?/*
? ? * ?方式one - localizedTitle
? ?if ([shortcutItem.localizedTitle isEqualToString:@"時(shí)尚之都"]) {
? ? ? ?tabBarVC.selectedIndex = 0;
? ?}else if ([shortcutItem.localizedTitle isEqualToString:@"知識(shí)海洋"]){ //知識(shí)海洋
? ? ? ?tabBarVC.selectedIndex = 1;
? ?}else{
? ? ? ?tabBarVC.selectedIndex = 2; //聯(lián)系的人
? ?}
? ?*/
? ?//方式two - type
? ?if ([shortcutItem.type isEqualToString:@"movie"]) { //時(shí)尚之都
? ? ? ?tabBarVC.selectedIndex = 0;
? ?}else if ([shortcutItem.type isEqualToString:@"book"]){ //知識(shí)海洋
? ? ? ?tabBarVC.selectedIndex = 1;
? ?}else{
? ? ? ?tabBarVC.selectedIndex = 2; //聯(lián)系的人
? ?}
}
2. Peek、Pop
經(jīng)過(guò)授權(quán)的應(yīng)用視圖控制器可響應(yīng)用戶不同的按壓力量,隨著按壓力量的增加,會(huì)有三個(gè)交互階段:
1.暗示預(yù)覽功能可用,會(huì)有一個(gè)虛化的效果
2.Peek:重按一下后出現(xiàn)的預(yù)覽,展示預(yù)覽的視圖以及快捷菜單
3.Pop:跳轉(zhuǎn)到預(yù)覽的視圖控制器,是在Peek后進(jìn)一步按壓后進(jìn)入預(yù)覽的視圖控制器
首先需遵守代理協(xié)議UIViewControllerPreviewingDelegate
@interface TableViewController ()
具體實(shí)現(xiàn)
- (NSMutableArray *)dataArray{
? ?if (!_dataArray) {
? ? ? ?_dataArray = [NSMutableArray new];
? ? ? ?int i = 0;
? ? ? ?while (i < 30) { ?//模擬數(shù)據(jù)
? ? ? ? ?[_dataArray addObject:@"http://www.baidu.com"];
? ? ? ? ? ?i ++;
? ? ? ?}
? ?}
? ?return _dataArray;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
? ?static NSString *cellID = @"cell";
? ?UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
? ?if (!cell) {
? ? ? ?cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
? ?}
? ?cell.textLabel.text = [NSString stringWithFormat:@"模擬第%ld個(gè)知識(shí)點(diǎn)",indexPath.row];
? ?/**
? ? * ?UIForceTouchCapability 檢測(cè)是否支持3D Touch
? ? */
? ?if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) { ?//支持3D Touch
? ? ? ?//系統(tǒng)所有cell可實(shí)現(xiàn)預(yù)覽(peek)
? ? ? ?[self registerForPreviewingWithDelegate:self sourceView:cell]; //注冊(cè)cell
? ?}
? ?return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
? ?[tableView deselectRowAtIndexPath:indexPath animated:YES];
? ?WebViewController *webVC = [WebViewController new];
? ?webVC.urlStr = self.dataArray[indexPath.row]; ?//傳數(shù)據(jù)
? ?webVC.hidesBottomBarWhenPushed = YES;
? ?[self.navigationController pushViewController:webVC animated:YES];
}
#pragma mark - UIViewControllerPreviewingDelegate
- (nullable UIViewController *)previewingContext:(id )previewingContext viewControllerForLocation:(CGPoint)location{
? ?WebViewController *webVC = [WebViewController new];
? ?//轉(zhuǎn)化坐標(biāo)
? ?location = [self.tableView convertPoint:location fromView:[previewingContext sourceView]];
? ?//根據(jù)locaton獲取位置
? ?NSIndexPath *path = [self.tableView indexPathForRowAtPoint:location];
? ?//根據(jù)位置獲取字典數(shù)據(jù)傳傳入控制器
? ?webVC.urlStr = self.dataArray[path.row];
? ?return webVC;
}
- (void)previewingContext:(id )previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
? ?viewControllerToCommit.hidesBottomBarWhenPushed = YES;
? ?[self.navigationController pushViewController:viewControllerToCommit animated:YES];
}
peek預(yù)覽.png
在還沒(méi)觸發(fā)Pop,上劃預(yù)覽視圖,則下面可去設(shè)置一些選項(xiàng)
//這個(gè)方法實(shí)現(xiàn)設(shè)置一些選項(xiàng)
- (NSArray> *)previewActionItems{
? ?//贊
? ?UIPreviewAction *itemOne = [UIPreviewAction actionWithTitle:@"贊" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
? ? ? ? [self showHint:@"已贊"];
? ?}];
? ?//舉報(bào)
? ?UIPreviewAction *itemTwo = [UIPreviewAction actionWithTitle:@"舉報(bào)" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
? ? ? ?[self showHint:@"舉報(bào)成功"];
? ?}];
? ?//收藏
? ?UIPreviewAction *itemThree = [UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
? ? ? ? [self showHint:@"收藏成功"];
? ?}];
? ? [self performSelector:@selector(hideHud) withObject:nil afterDelay:1];
? ?//創(chuàng)建一個(gè)組包含操作
// ? ?UIPreviewActionGroup *group = [UIPreviewActionGroup actionGroupWithTitle:@"" style:UIPreviewActionStyleDefault actions:@[@"",@""]];
? ?return @[itemOne,itemTwo,itemThree];
}
- (void)viewDidLoad {
? ?[super viewDidLoad];
? ?//控制器view置為webView ?- 請(qǐng)求傳入的url
? ?[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_urlStr]]];
}
peek設(shè)置預(yù)覽的一些觸發(fā)操作.png
3. Force Properties
iOS9.0為我們提供了一個(gè)新的交互參數(shù):力度。我們可以檢測(cè)某一交互的力度值,來(lái)做相應(yīng)的交互處理
// Force of the touch, where 1.0 represents the force of an average touch
@property(nonatomic,readonly) CGFloat force NS_AVAILABLE_IOS(9_0);
// Maximum possible force with this input mechanism
@property(nonatomic,readonly) CGFloat maximumPossibleForce NS_AVAILABLE_IOS(9_0);
//聯(lián)系的人界面 測(cè)試壓力大小對(duì)其view的背景顏色改變
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
? ?UITouch *touch = touches.anyObject;
? ?/**
? ? * ?maximumPossibleForce 最大 6.67
? ? */
? ?NSLog(@"%.2f,%2f",touch.force,touch.maximumPossibleForce); //iOS 9.0之后
? ?CGFloat radio = touch.force / touch.maximumPossibleForce;
? ?self.view.backgroundColor = [UIColor colorWithRed:radio green:radio blue:radio alpha:1];
}
//時(shí)尚之都界面 測(cè)試壓力改變?cè)谄鋠iew畫圓大小
- (void)drawRect:(CGRect)rect {
? ?[[UIColor blueColor] set];
? ?[self.path fill];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
? ?UITouch *touch = touches.anyObject;
? ?UIBezierPath *path = [UIBezierPath bezierPath];
? ?//壓力系數(shù)為半徑 觸摸點(diǎn)為圓心
? ?[path addArcWithCenter:[touch locationInView:self] radius:touch.force * 25 startAngle:0 endAngle:2 * M_PI clockwise:YES];
? ?self.path = path;
? ?[self setNeedsDisplay];
}
依據(jù)按壓力度畫圓.png
依據(jù)按壓力度大小改變控制器view的背景顏色.png
整體效果圖
3D Touch
基本上涉及到3D Touch的知識(shí)點(diǎn)就上面這些吧,也可以看下官網(wǎng)的3D Touch
?