iOS 3D Touch之Peek &Pop

2019-04-17  本文已影响0人  邓布利多教授

硬件支持 iPhone 6s以上

系统支持iOS9以上

本文应用场景ViewController内

我们创建一个View,用来触发Touch功能

UIView *vPP = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
vPP.center = self.view.center;
vPP.backgroundColor = [UIColor redColor];
[self.view addSubview:vPP];

开始触发功能之前,要先知道当前设备是否支持Touch功能,如果支持Touch功能就为指定视图注册delegate,判断注册如下:

//判断设备是否支持3D-Touch功能
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        
        //如果支持3D-Touch功能,则注册3D-Touch
        [self registerForPreviewingWithDelegate:(id)self sourceView:vPP];
        
}

系统提供的状态枚举

typedef NS_ENUM(NSInteger, UIForceTouchCapability) {
     UIForceTouchCapabilityUnknown = 0,     //无法识别
     UIForceTouchCapabilityUnavailable = 1, //不支持
     UIForceTouchCapabilityAvailable = 2    //支持
     };

这里解释一下peek和pop两个手势,触发peek的条件是重按我们预先创建的那个view,当设备明显震动一下的时候会弹出一个页面,这个页面就是peek出来的页面;peek页面出来的时候手指不要移动,继续加大力度重按,这个时候设备又会震动一下,然后会弹出一个页面,这个页面就是pop出来的页面。
peek出来的页面如果手指向上滑动会出现Preview Actions

PeekVController

为了效果,简单实现

在- (void)viewDidLoad;添加一个UIImageView显示一张图片,这个肯定不用上代码了,都会写的……
然后重点来了,创建preview的ActionItems,你可以根据需求创建一组或者多组action,我们这里为了演示,创建了两组,代码如下:

#pragma mark - Preview Actions
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
    
    /*
     
     创建UIPreviewAction
     
     typedef NS_ENUM(NSInteger,UIPreviewActionStyle) {
     UIPreviewActionStyleDefault=0,     //默认
     UIPreviewActionStyleSelected,      //选中
     UIPreviewActionStyleDestructive,   //标注
     } NS_ENUM_AVAILABLE_IOS(9_0);
     
     */
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Action 1"
                                                          style:UIPreviewActionStyleDefault
                                                        handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 1 selected");
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"Action 2"
                                                          style:UIPreviewActionStyleSelected
                                                        handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 2 selected");
    }];
    
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"Action 3"
                                                          style:UIPreviewActionStyleDestructive
                                                        handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 3 selected");
    }];
    
    UIPreviewAction *tap1 = [UIPreviewAction actionWithTitle:@"tap 1"
                                                       style:UIPreviewActionStyleDefault
                                                     handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 1 selected");
    }];
    
    UIPreviewAction *tap2 = [UIPreviewAction actionWithTitle:@"tap 2"
                                                       style:UIPreviewActionStyleSelected
                                                     handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 2 selected");
    }];
    
    UIPreviewAction *tap3 = [UIPreviewAction actionWithTitle:@"tap 3"
                                                       style:UIPreviewActionStyleDestructive
                                                     handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 3 selected");
    }];
    
    //添加到UIPreviewActionGroup中
    NSArray *actions = @[action1, action2, action3];
    NSArray *taps = @[tap1, tap2, tap3];
    
    UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group"
                                                                        style:UIPreviewActionStyleDefault
                                                                      actions:actions];
    
    UIPreviewActionGroup *group2 = [UIPreviewActionGroup actionGroupWithTitle:@"Tap Group"
                                                                        style:UIPreviewActionStyleDefault
                                                                      actions:taps];
    NSArray *group = @[group1, group2];
    
    return group;
    
}

PopVController

#import "PopVController.h"

@interface PopVController ()

@property (nonatomic, strong) UITapGestureRecognizer *tap;

@end

@implementation PopVController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255) / 255.0 green:arc4random_uniform(255) / 255.0 blue:arc4random_uniform(255) / 255.0 alpha:1];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.image = [UIImage imageNamed:@"linshitupian"];
    [self.view addSubview:imageView];
    [self.view addGestureRecognizer:self.tap];
}

- (void)dismiss{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (UITapGestureRecognizer *)tap{
    if (!_tap) {
        _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    }
    return _tap;
}

@end

好,PeekVController和PopVController的代码都实现了,现在把这两个Controller引入到ViewController
在ViewController实现两个代理方法,一个实现peek手势,一个实现pop手势,分别如下:

#pragma mark - peek
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    
    PeekVController *pkvc = [[PeekVController alloc] init];
    return pkvc;
    
}
#pragma mark - pop
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
    
    PopVController *ppvc = [[PopVController alloc] init];
    [self showViewController:ppvc sender:self];
    
}

ok,就这么简单,复制黏贴去试一下吧

如果还想了解手机桌面的3D Touch功能的可以看这里

上一篇 下一篇

猜你喜欢

热点阅读