iOS 指壓即達集成iOS9里的3D Touch的方法
1.前言
隨著6S的到來,3DTouch被各大熱門APP迅速普及,博主親自體驗后,發(fā)現(xiàn)使用便捷性大幅提高,隨后自己照著文檔,寫了個Demo出來,分享給大家,希望能對有需要的朋友提供有一些幫助。
2.如何使用3D Touch?
2.1.主界面重按APP圖標,彈出Touch菜單

在AppleDelegate文件中的程序入口處配置:
didFinishLaunchingWithOptions
//給App圖標添加3D Touch菜單 //拍照 //菜單圖標 UIApplicationShortcutIcon *iconCamera = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd]; //菜單文字 UIMutableApplicationShortcutItem *itemCamera = [[UIMutableApplicationShortcutItem alloc] initWithType:@"1" localizedTitle:@"拍照"]; //綁定信息到指定菜單 itemCamera.icon = iconCamera; //相冊 //菜單圖標 UIApplicationShortcutIcon *iconPhotoLibrary = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch]; //菜單文字 UIMutableApplicationShortcutItem *itemPhotoLibrary = [[UIMutableApplicationShortcutItem alloc] initWithType:@"2" localizedTitle:@"相冊"]; //綁定信息到指定菜單 itemPhotoLibrary.icon = iconPhotoLibrary; //綁定到App icon application.shortcutItems = @[itemCamera,itemPhotoLibrary];
彈出菜單,我們需要讓用戶點擊后跳轉指定頁面
這里我們會用到AppDelegate里新增加的一個方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler;
讓后我們需要在這個方法里做跳轉的操作
//照相type
if ([shortcutItem.type isEqualToString:@"1"]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
picker.allowsEditing = YES;//設置可編輯
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.window.rootViewController presentViewController:picker animated:YES completion:nil];//進入照相界面
}
//相冊type
if ([shortcutItem.type isEqualToString:@"2"]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
picker.allowsEditing = YES;//設置可編輯
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.window.rootViewController presentViewController:picker animated:YES completion:nil];//進入圖片庫
點擊后分別會進入相機和相冊


2.2. 3DTouch輕按預覽功能,預覽時底部菜單的添加
首先我們要把輕按預覽和長按手勢區(qū)分開來,這里要在初始化時做一個基本的檢測。
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];
}
然后我們需要實現(xiàn) UIViewControllerPreviewingDelegate的協(xié)議
@interface ViewController () <UIViewControllerPreviewingDelegate>
//然后實現(xiàn)代理方法
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location;
#pragma mark >> 3D touch 代理方法
//輕按進入浮動預覽頁面
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
//注意這里我因為測試,沒做具體的位置處理,如果需要定位到具體的圖片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;
}
完成后可以實現(xiàn)基本的預覽效果:


最后我們加上一個
預覽時下滑底部菜單的添加
在我們剛剛創(chuàng)建的預覽控制器ASPreviewViewController里實現(xiàn) UIViewControllerPreviewingDelegate的協(xié)議
然后重寫它的代理方法
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems;
//預覽頁面 底部Action Items
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
UIPreviewAction *p1 =[UIPreviewAction actionWithTitle:@"分享" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"點擊了分享");
}];
UIPreviewAction *p2 =[UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"點擊了收藏");
}];
NSArray *actions = @[p1,p2];
return actions;
}

以上所述是小編給大家介紹的iOS 指壓即達集成iOS9里的3D Touch的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
iOS優(yōu)化UITableViewCell高度計算的一些事兒
這iOS開發(fā)中對于UITableViewCell高度自適應的文章已經(jīng)很多很多,但都不是自己所需要的,下面篇文章主要給大家介紹了關于iOS優(yōu)化UITableViewCell高度計算的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-11-11
IOS使用NSUserDefault去實現(xiàn)界面?zhèn)髦岛蛿?shù)據(jù)存儲
這篇文章主要介紹了IOS使用NSUserDefault去實現(xiàn)界面?zhèn)髦岛蛿?shù)據(jù)存儲的相關資料,需要的朋友可以參考下2017-07-07
兼容iOS 10 升級xcode8出現(xiàn)的問題及一些適配問題的解決方案
這篇文章主要介紹了兼容iOS 10 升級xcode8出現(xiàn)的問題及一些適配問題的解決方案,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
iOS實現(xiàn)文件切片儲存并且上傳(仿斷點續(xù)傳機制)
這篇文章主要給大家介紹了關于iOS實現(xiàn)文件切片儲存并上傳仿斷點續(xù)傳機制的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-12-12

