3DTouch--2

苹果太贱了! 3D Touch 只能在真机上面试,模拟器没办法玩!

-------------

描述有点粗燥。。。。。有6S 在手上玩得童鞋会更加清楚,只有玩过才更加体验到。


首先 有几个要知道的手势

第一, 在点击app icon 的手长按 并且用力一点(用点力不然没效果,不会弄坏手机,坏了也不是我的,哈哈!) 就会出现 几个Item。

第二,(1)在app 里面 长按 也要用力往下压 跟着就会可以弹出 自定义的 ViewController。这个时候如果你放手了那么就会消失。

           (2)如果  长按 往下压 弹出了自定义的ViewController 之后跟着网上移动,就可以出现 选择Action。

第三,如果 长按 往下压 弹出了自定义的ViewController,然后更加 用力一点 比 弹出的ViewController的力度 更加大一点 那么  自定义的这个ViewController 就会 相当于push 进来了。

首先来一个获取版本号,因为3D Touch 只有在iOS9 才会有,在后面演示的代码就不上这个判断。

 

  1. #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]  


首先在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  去创建 item ,这几个item 就是在点击icon 的时候出现的.

 

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     // Override point for customization after application launch.  
  3.       
  4.     [self createItem];  
  5.     UIApplicationShortcutItem *item = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];  
  6.     if (item)  
  7.     {  
  8.         NSLog(@"We've launched from shortcut item: %@", item.localizedTitle);  
  9.     }  
  10.     else  
  11.     {  
  12.         NSLog(@"We've launched properly.");  
  13.     }  
  14.   
  15.     return YES;  
  16. }  
  17.   
  18. - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {  
  19.       
  20.     // react to shortcut item selections  
  21.     NSLog(@"A shortcut item was pressed. It was %@.", shortcutItem.localizedTitle);  
  22. }  



 创建item 可以在plist 里面定义,也可以用代码去写。可以带icon 也可以不带icon。

至于有些app 在 touch 之后显示的icon 在左边或者右边,其实这个是跟你的app 放在你手机的位置有关系,这个iOS 自动处理掉。

  1. -(void) createItem  
  2. {  
  3.     //自定义icon 的初始化方法  
  4. //    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"your_icon"];  
  5. //    UIMutableApplicationShortcutItem *item0 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"com.your.helloWorld" localizedTitle:@"Title" localizedSubtitle:@"sub Title" icon:icon1 userInfo:nil];  
  6.     //这种是随意没有icon 的  
  7.     UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"test.com.A" localizedTitle:@"三条A"];  
  8.     UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"test.com.B" localizedTitle:@"三条B"];  
  9.     UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"test.com.C" localizedTitle:@"三条C"];  
  10.       
  11.     NSArray *addArr = @[item2,item3,item1];  
  12.     //为什么这两句话可以不用,因为我们可以在plist 里面 加入 UIApplicationShortcutItems  
  13. //    NSArray *existArr = [UIApplication sharedApplication].shortcutItems;  
  14. //    [UIApplication sharedApplication].shortcutItems = [existArr arrayByAddingObjectsFromArray:addArr];  
  15.     [UIApplication sharedApplication].shortcutItems = addArr;  
  16. }  


-------------------------------------------------------------------------- 分割线----------------------------------------------------------

接着这里要说的是 在 长按touch ViewController  弹出 自定义的ViewContoller

首先 在 ViewController.m 里面加入(这个就是要手指 长按并且要往下压的ViewController)

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.       
  6.     //首先要判断一下 压力感是否有效,跟着注册delegate  
  7.     [self check3DTouch];  
  8. }  
  9.   
  10. - (void)check3DTouch  
  11. {  
  12.       
  13.     // register for 3D Touch (if available)  
  14.     if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)  
  15.     {  
  16.         [self registerForPreviewingWithDelegate:self sourceView:self.view];  
  17.         NSLog(@"3D Touch  可用!");  
  18.     }  
  19.     else  
  20.     {  
  21.         NSLog(@"3D Touch 无效");  
  22.     }  
  23. }  


  再写上你想要 弹出的ViewController

  1. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location  
  2. {  
  3.     // check if we're not already displaying a preview controller  
  4.     //SecViewController 是要弹出悬浮展示的ViewController  
  5.     if ([self.presentedViewController isKindOfClass:[SecViewController class]]) {  
  6.         return nil;  
  7.     }  
  8.   
  9.     SecViewController *sec = [[SecViewController alloc] init];  
  10.   
  11.     return sec;  
  12.       
  13. }  



未了方便的演示 我们在 SecViewController 里面加上一个返回的手势

  1. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissMe)];  
  2.             [self.view addGestureRecognizer:tap];  
  3.   
  4. - (void)dismissMe{  
  5.     // dismiss this view controller  
  6.     [self dismissViewControllerAnimated:YES completion:nil];  
  7. }  


当弹出自定义的SecViewController 之后 然后我们往上移动那么就会出现Action

这代码是写在SecViewController 里面的

  1. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {  
  2.       
  3.     // setup a list of preview actions  
  4.     UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"这里可以做你想要做的事情的Aciton" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {  
  5.         NSLog(@"click");  
  6.     }];  
  7.       
  8.     // add them to an arrary  
  9.     //想要显示多个就定义多个 UIPreviewAction  
  10.     NSArray *actions = @[action1];  
  11.       
  12.     // and return them (return the array of actions instead to see all items ungrouped)  
  13.     return actions;  
  14. }  


-------------------------------------------------------------------------- 分割线----------------------------------------------------------

这个全屏展示方法(相当于push SecViewController) ,这个方法是要 更加给大点力度往下压的时候 才会出发的 这个方法写在ViewController 里面

  1. //这个方法是在什么时候出发呢?就是 给更加大的力度的时候进去 全屏状态  
  2. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {  
  3.       
  4.     // deep press: bring up the commit view controller (pop)  
  5.     [self showViewController:viewControllerToCommit sender:self];  
  6. }  






初次接触只有学习到这些东西了。。。

有新的东西后续补充


posted @ 2015-12-21 16:37  来事啊  阅读(142)  评论(0编辑  收藏  举报