iOS 指壓即達(dá)集成iOS9里的3D Touch的方法
1.前言
隨著6S的到來(lái),3DTouch被各大熱門(mén)APP迅速普及,博主親自體驗(yàn)后,發(fā)現(xiàn)使用便捷性大幅提高,隨后自己照著文檔,寫(xiě)了個(gè)Demo出來(lái),分享給大家,希望能對(duì)有需要的朋友提供有一些幫助。
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; //相冊(cè) //菜單圖標(biāo) UIApplicationShortcutIcon *iconPhotoLibrary = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch]; //菜單文字 UIMutableApplicationShortcutItem *itemPhotoLibrary = [[UIMutableApplicationShortcutItem alloc] initWithType:@"2" localizedTitle:@"相冊(cè)"]; //綁定信息到指定菜單 itemPhotoLibrary.icon = iconPhotoLibrary; //綁定到App icon application.shortcutItems = @[itemCamera,itemPhotoLibrary];
彈出菜單,我們需要讓用戶點(diǎn)擊后跳轉(zhuǎn)指定頁(yè)面
這里我們會(huì)用到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)入照相界面 } //相冊(cè)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)入圖片庫(kù)
點(diǎn)擊后分別會(huì)進(jìn)入相機(jī)和相冊(cè)
2.2. 3DTouch輕按預(yù)覽功能,預(yù)覽時(shí)底部菜單的添加
首先我們要把輕按預(yù)覽和長(zhǎng)按手勢(shì)區(qū)分開(kāi)來(lái),這里要在初始化時(shí)做一個(gè)基本的檢測(cè)。
nterface ViewController () <UIViewControllerPreviewingDelegate> { UILongPressGestureRecognizer *_longPress; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo)]; _longPress = longPressGr; } //檢測(cè)頁(yè)面是否處于3DTouch - (void)check3DTouch{ if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) { [self registerForPreviewingWithDelegate:self sourceView:self.view]; NSLog(@"3D Touch 開(kāi)啟"); //長(zhǎng)按停止 _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)入浮動(dòng)預(yù)覽頁(yè)面 - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{ //注意這里我因?yàn)闇y(cè)試,沒(méi)做具體的位置處理,如果需要定位到具體的圖片Cell位置的話,可以用location通過(guò)tableView的convertPoint來(lái)取到指定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é)議
然后重寫(xiě)它的代理方法
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems; //預(yù)覽頁(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; }
以上所述是小編給大家介紹的iOS 指壓即達(dá)集成iOS9里的3D Touch的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS優(yōu)化UITableViewCell高度計(jì)算的一些事兒
這iOS開(kāi)發(fā)中對(duì)于UITableViewCell高度自適應(yīng)的文章已經(jīng)很多很多,但都不是自己所需要的,下面篇文章主要給大家介紹了關(guān)于iOS優(yōu)化UITableViewCell高度計(jì)算的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-11-11IOS使用NSUserDefault去實(shí)現(xiàn)界面?zhèn)髦岛蛿?shù)據(jù)存儲(chǔ)
這篇文章主要介紹了IOS使用NSUserDefault去實(shí)現(xiàn)界面?zhèn)髦岛蛿?shù)據(jù)存儲(chǔ)的相關(guān)資料,需要的朋友可以參考下2017-07-07兼容iOS 10 升級(jí)xcode8出現(xiàn)的問(wèn)題及一些適配問(wèn)題的解決方案
這篇文章主要介紹了兼容iOS 10 升級(jí)xcode8出現(xiàn)的問(wèn)題及一些適配問(wèn)題的解決方案,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09iOS藍(lán)牙設(shè)備名稱緩存問(wèn)題的解決方法
這篇文章主要給大家介紹了關(guān)于iOS藍(lán)牙設(shè)備名稱緩存問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09詳解iOS中多倒計(jì)時(shí)場(chǎng)景的解決方案
在我們開(kāi)發(fā)APP的過(guò)程中,或多或少都遇到過(guò)需要使用倒計(jì)時(shí)的場(chǎng)景,這篇文章主要介紹了詳解iOS中多倒計(jì)時(shí)場(chǎng)景的解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11利用iOS開(kāi)發(fā)實(shí)現(xiàn)翻轉(zhuǎn)撲克牌動(dòng)畫(huà)的方法
這篇文章主要給大家介紹了關(guān)于利用iOS開(kāi)發(fā)實(shí)現(xiàn)翻撲克牌動(dòng)畫(huà)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07iOS實(shí)現(xiàn)文件切片儲(chǔ)存并且上傳(仿斷點(diǎn)續(xù)傳機(jī)制)
這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)文件切片儲(chǔ)存并上傳仿斷點(diǎn)續(xù)傳機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12