您好,登錄后才能下訂單哦!
這篇文章主要介紹了iOS怎么使用3D Touch,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
1.前言
隨著6S的到來,3DTouch被各大熱門APP迅速普及,博主親自體驗(yàn)后,發(fā)現(xiàn)使用便捷性大幅提高,隨后自己照著文檔,寫了個(gè)Demo出來。
2.如何使用3D Touch?
2.1.主界面重按APP圖標(biāo),彈出Touch菜單
在AppleDelegate文件中的程序入口處配置:
didFinishLaunchingWithOptions
//給App圖標(biāo)添加3D Touch菜單 //拍照 //菜單圖標(biāo) UIApplicationShortcutIcon *iconCamera = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd]; //菜單文字 UIMutableApplicationShortcutItem *itemCamera = [[UIMutableApplicationShortcutItem alloc] initWithType:@"1" localizedTitle:@"拍照"]; //綁定信息到指定菜單 itemCamera.icon = iconCamera; //相冊 //菜單圖標(biāo) UIApplicationShortcutIcon *iconPhotoLibrary = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch]; //菜單文字 UIMutableApplicationShortcutItem *itemPhotoLibrary = [[UIMutableApplicationShortcutItem alloc] initWithType:@"2" localizedTitle:@"相冊"]; //綁定信息到指定菜單 itemPhotoLibrary.icon = iconPhotoLibrary; //綁定到App icon application.shortcutItems = @[itemCamera,itemPhotoLibrary];
彈出菜單,我們需要讓用戶點(diǎn)擊后跳轉(zhuǎn)指定頁面
這里我們會用到AppDelegate里新增加的一個(gè)方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler;
讓后我們需要在這個(gè)方法里做跳轉(zhuǎn)的操作
//照相type if ([shortcutItem.type isEqualToString:@"1"]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化 picker.allowsEditing = YES;//設(shè)置可編輯 picker.sourceType = UIImagePickerControllerSourceTypeCamera; [self.window.rootViewController presentViewController:picker animated:YES completion:nil];//進(jìn)入照相界面 } //相冊type if ([shortcutItem.type isEqualToString:@"2"]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化 picker.allowsEditing = YES;//設(shè)置可編輯 picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self.window.rootViewController presentViewController:picker animated:YES completion:nil];//進(jìn)入圖片庫
點(diǎn)擊后分別會進(jìn)入相機(jī)和相冊
2.2. 3DTouch輕按預(yù)覽功能,預(yù)覽時(shí)底部菜單的添加
首先我們要把輕按預(yù)覽和長按手勢區(qū)分開來,這里要在初始化時(shí)做一個(gè)基本的檢測。
nterface ViewController () <UIViewControllerPreviewingDelegate> { UILongPressGestureRecognizer *_longPress; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo)]; _longPress = longPressGr; } //檢測頁面是否處于3DTouch - (void)check3DTouch{ if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) { [self registerForPreviewingWithDelegate:self sourceView:self.view]; NSLog(@"3D Touch 開啟"); //長按停止 _longPress.enabled = NO; }else{ _longPress.enabled = YES; } } - (void)viewWillAppear:(BOOL)animated{ [self check3DTouch]; }
然后我們需要實(shí)現(xiàn) UIViewControllerPreviewingDelegate的協(xié)議
@interface ViewController () <UIViewControllerPreviewingDelegate>
//然后實(shí)現(xiàn)代理方法 - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location; #pragma mark >> 3D touch 代理方法 //輕按進(jìn)入浮動預(yù)覽頁面 - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{ //注意這里我因?yàn)闇y試,沒做具體的位置處理,如果需要定位到具體的圖片Cell位置的話,可以用location通過tableView的convertPoint來取到指定Cell ASPreviewViewController *vc = [[ASPreviewViewController alloc] init]; vc.view.frame = self.view.frame; UIImageView *er = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"123.png"]]; vc.view = er; return vc; }
完成后可以實(shí)現(xiàn)基本的預(yù)覽效果:
最后我們加上一個(gè)
預(yù)覽時(shí)下滑底部菜單的添加
在我們剛剛創(chuàng)建的預(yù)覽控制器ASPreviewViewController里實(shí)現(xiàn) UIViewControllerPreviewingDelegate的協(xié)議
然后重寫它的代理方法
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems; //預(yù)覽頁面 底部Action Items - (NSArray<id<UIPreviewActionItem>> *)previewActionItems{ UIPreviewAction *p1 =[UIPreviewAction actionWithTitle:@"分享" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"點(diǎn)擊了分享"); }]; UIPreviewAction *p2 =[UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"點(diǎn)擊了收藏"); }]; NSArray *actions = @[p1,p2]; return actions; }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“iOS怎么使用3D Touch”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。