IOS 3DTouch技術(shù)使用

IOS 3DTouch技術(shù)使用

簡(jiǎn)介

3D Touch是一種立體觸控技術(shù),被蘋果稱為新一代多點(diǎn)觸控技術(shù),是在Apple Watch上采用的Force Touch,屏幕可感應(yīng)不同的感壓力度觸控。3D Touch,蘋果iPhone 6s的新功能。有PeekPop兩種新手勢(shì)。3D-Touch技術(shù),相對(duì)于多點(diǎn)觸摸在平面二維空間的操作,3D-Touch技術(shù)增加了對(duì)力度和手指面積的感知,可以通過長(zhǎng)按快速預(yù)覽/查看你想要的短信/圖片/超鏈接等內(nèi)容,PeekPop手勢(shì)的響應(yīng)時(shí)間可迅捷到10ms15ms

應(yīng)用外3DTouch

介紹

應(yīng)用外3DTouch技術(shù)是指應(yīng)用處于桌面ICON快捷菜單,這種設(shè)計(jì)極大的提高應(yīng)用使用的便捷性,更加快速定位到常用功能的使用。樣式如下圖:


Wechat
JD

創(chuàng)建方式

Info.plist方式實(shí)現(xiàn)3DTouch技術(shù)

UIApplicationShortcutItems:數(shù)組中的元素就是我們的那些快捷選項(xiàng)標(biāo)簽。
UIApplicationShortcutItemTitle:標(biāo)簽標(biāo)題(必填)
UIApplicationShortcutItemType:標(biāo)簽的唯一標(biāo)識(shí) (必填)
UIApplicationShortcutItemIconType:使用系統(tǒng)圖標(biāo)的類型,如搜索、定位、home等(可選)
UIApplicationShortcutItemIcon File:使用項(xiàng)目中的圖片作為標(biāo)簽圖標(biāo) (可選)
UIApplicationShortcutItemSubtitle:標(biāo)簽副標(biāo)題 (可選)
UIApplicationShortcutItemUserInfo:字典信息,如傳值使用 (可選)
Info.plist方式實(shí)現(xiàn)3DTouch技術(shù)
Info.plist方式實(shí)現(xiàn)3DTouch效果圖

純代碼方式實(shí)現(xiàn)3DTouch技術(shù)。

// 自定義圖標(biāo)
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"pic1"];

UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"pic2"];

UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"pic3"];

UIApplicationShortcutIcon *icon4 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"pic4"];
    

// 創(chuàng)建帶著有自定義圖標(biāo)item
UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"pic1" localizedTitle:@"進(jìn)入pic1" localizedSubtitle:@"自定義圖標(biāo)pic1" icon:icon1 userInfo:nil];

UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"pic2" localizedTitle:@"進(jìn)入pic2" localizedSubtitle:@"自定義圖標(biāo)pic2" icon:icon2 userInfo:nil];
    
UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"pic3" localizedTitle:@"進(jìn)入pic3" localizedSubtitle:@"自定義圖標(biāo)pic3" icon:icon3 userInfo:nil];
    
UIMutableApplicationShortcutItem *item4 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"pic4" localizedTitle:@"進(jìn)入pic4" localizedSubtitle:@"自定義圖標(biāo)pic4" icon:icon4 userInfo:nil];
    
    
[[UIApplication sharedApplication] setShortcutItems:@[item1,item2,item3,item4]];

UIMutableApplicationShortcutItem初始化方法介紹如下:


- (instancetype)initWithType:(NSString *)type 
                 localizedTitle:(NSString *)localizedTitle               
                 localizedSubtitle:(nullable NSString *)localizedSubtitle 
                 icon:(nullable UIApplicationShortcutIcon *)icon                 
                 userInfo:(nullable NSDictionary *)userInfo 
  • typeitem的唯一標(biāo)識(shí);
  • localizedTitle :是item的標(biāo)題;
  • localizedSubtitleitem的副標(biāo)題
  • iconitem icon設(shè)置的圖片
  • userInfo :item所包含的信信息,類型是字典。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 首先判斷是否支持3DTouch
    if(self.window.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
    {
        // 創(chuàng)建3DTouch模型
    }
    return YES;
}
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    // 1.獲得shortcutItem的type type就是初始化shortcutItem的時(shí)候傳入的唯一標(biāo)識(shí)符
    NSString *type = shortcutItem.type;

    //2.可以通過type來判斷點(diǎn)擊的是哪一個(gè)快捷按鈕 并進(jìn)行每個(gè)按鈕相應(yīng)的點(diǎn)擊事件
    if ([type isEqualToString:@"pic1"]) {
        // do something
    }else if ([type isEqualToString:@"pic2"]){
        // do something
    }else if ([type isEqualToString:@"pic3"]){
        // do something
    }else if ([type isEqualToString:@"pic4"]){
        // do something
    } 
}
純代碼方式實(shí)現(xiàn)3DTouch效果圖

應(yīng)用內(nèi)的3DTouch技術(shù)的應(yīng)用

我們先控制器中在創(chuàng)建一個(gè)tableView,并遵守代理UIViewControllerPreviewingDelegate,再創(chuàng)建一個(gè)要跳轉(zhuǎn)的目標(biāo)控制器。代碼如下:

#import "ViewController.h"
#import "HomeDetailController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIViewControllerPreviewingDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *datas;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"3DTouch_text";
    
    _datas = [[NSMutableArray alloc]init];
    for (NSInteger i = 0; i < 20; i++) {
        [_datas addObject:[NSString stringWithFormat:@"cell_%li",(long)I]];
    }
    
    self.tableView.frame = self.view.bounds;
    
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"myCell"];
    
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
    }
    cell.textLabel.text = _datas[indexPath.row];
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        NSLog(@"3D Touch  可用!");
        //給cell注冊(cè)3DTouch的peek(預(yù)覽)和pop功能 ,注冊(cè)(在哪個(gè)頁面上使用該功能就注冊(cè)在哪個(gè)頁面上)
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    } else {
        NSLog(@"3D Touch 無效");
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeDetailController *vc = [[HomeDetailController alloc]init];
    
    vc.name = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    
    [self.navigationController pushViewController:vc animated:YES];
}

//2.第二步
#pragma mark - UIViewControllerPreviewingDelegate(實(shí)現(xiàn)代理的方法)
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    
    HomeDetailController *vc = [[HomeDetailController alloc]init];
    
    //獲取按壓的cell所在行,[previewingContext sourceView]就是按壓的那個(gè)視圖
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
    
    //設(shè)定預(yù)覽的界面
    
    vc.preferredContentSize = CGSizeMake(0.0f,500.0f);
    vc.name = [NSString stringWithFormat:@"Cell_%ld,用力按進(jìn)來,或者往上推",(long)indexPath.row];
    
    //調(diào)整不被虛化的范圍,按壓的那個(gè)cell不被虛化(輕輕按壓時(shí)周邊會(huì)被虛化,再少用力展示預(yù)覽,再加力跳頁至設(shè)定界面)
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width,40);
    previewingContext.sourceRect = rect;
    
    return vc;
    
}

- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    [self.navigationController pushViewController:viewControllerToCommit animated:YES];
    
}


@end



目標(biāo)控制器中的代碼實(shí)現(xiàn):

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    // setup a list of preview actions
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"關(guān)注" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"關(guān)注");
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"收藏");
    }];
    
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"分享" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"分享");
    }];
    
    NSArray *actions = @[action1,action2,action3];
    
       return actions;
}

peek(預(yù)覽)
peek(預(yù)覽)

博客鏈接

IOS 3DTouch技術(shù)使用
IOS 3DTouch 代碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容