iOS 3D Touch (UIApplicationShortcutItem、UIViewControllerPreviewing、UIPreviewAction)

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

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

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


首先 有几个要知道的手势

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

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

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

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

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

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


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

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [self createItem];
    UIApplicationShortcutItem *item = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
    if (item)
    {
        NSLog(@"We've launched from shortcut item: %@", item.localizedTitle);
    }
    else
    {
        NSLog(@"We've launched properly.");
    }

    return YES;
}

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    
    // react to shortcut item selections
    NSLog(@"A shortcut item was pressed. It was %@.", shortcutItem.localizedTitle);
}




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

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

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


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

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

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //首先要判断一下 压力感是否有效,跟着注册delegate
    [self check3DTouch];
}

- (void)check3DTouch
{
    
    // register for 3D Touch (if available)
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
    {
        [self registerForPreviewingWithDelegate:self sourceView:self.view];
        NSLog(@"3D Touch  可用!");
    }
    else
    {
        NSLog(@"3D Touch 无效");
    }
}

  再写上你想要 弹出的ViewController
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    // check if we're not already displaying a preview controller
    //SecViewController 是要弹出悬浮展示的ViewController
    if ([self.presentedViewController isKindOfClass:[SecViewController class]]) {
        return nil;
    }

    SecViewController *sec = [[SecViewController alloc] init];

    return sec;
    
}



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

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissMe)];
            [self.view addGestureRecognizer:tap];

- (void)dismissMe{
    // dismiss this view controller
    [self dismissViewControllerAnimated:YES completion:nil];
}

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

这代码是写在SecViewController 里面的

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

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

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

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





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

有新的东西后续补充




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值