iOS 3Dtouch 开发

先附上Demo:https://git.oschina.net/1203556555/JY3DtouchDemo.git

转载请注明出处

  1. UIApplicationShortcutItems即用3Dtouch在app图标呼出一个菜单

   142643_gIeA_2556411.jpg

  实现由两种方法:静态菜单和动态菜单

  静态菜单是在plist中添加,优点是在第一次打开app前就生效

 143334_XHy7_2556411.png  

  动态菜单是用代码生成,缺点是在第一次打开app前无法生效故不做演示

  实现点击菜单直接打开指定控制器  

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    if ([shortcutItem.localizedTitle isEqualToString:@"我的二维码"]) {
        //跳转到第一个tabbar
        self.rootTabbarCtr.selectedIndex = 0;
        JYTwoDimensionCodeViewController *vc = [[JYTwoDimensionCodeViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.homeViewController.navigationController pushViewController:vc animated:NO];
    }
    else if([shortcutItem.localizedTitle isEqualToString:@"扫一扫"]){
        //跳转到第一个tabbar
        self.rootTabbarCtr.selectedIndex = 0;
        JYScanViewController *vc = [[JYScanViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.homeViewController.navigationController pushViewController:vc animated:NO];
    }
}

2.peek和pop

 //实现peekpop手势:

 //1、遵守协议 UIViewControllerPreviewingDelegate

 //2、注册    [self registerForPreviewingWithDelegate:self sourceView:self.view];

 //3、实现代理方法

  遵守协议

@interface JYMoreViewController ()<UITableViewDelegate,UITableViewDataSource,UIViewControllerPreviewingDelegate>

  //peekpop相关属性


@property (nonatomic, assign) CGRect sourceRect;
@property (nonatomic, strong) NSIndexPath *selectedPath;

  注册

- (void)viewDidLoad {
    [super viewDidLoad];
    //注册pop,peek方法
    [self registerForPreviewingWithDelegate:self sourceView:self.view];
}

  实现peek,pop方法

//peek手势
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint) location
{
    // 获取用户手势点所在cell的下标。同时判断手势点是否超出tableView响应范围。
    if (![self getShouldShowRectAndIndexPathWithLocation:location]) return nil;
    
    //弹出视图的初始位置,sourceRect是peek触发时的高亮区域。这个区域内的View会高亮显示,其余的会模糊掉
    previewingContext.sourceRect = self.sourceRect;
    
    UIViewController *childVC = [[JYPopViewController alloc] init];
    return childVC;
}

//pop手势
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    viewControllerToCommit.hidesBottomBarWhenPushed = YES;
//    [self tableView:self.moreTableView didSelectRowAtIndexPath:self.selectedPath];
    [self showViewController:viewControllerToCommit sender:nil];
}

//获取用户手势点所在cell的下标,同时判断手势点是否超出tableview的范围
- (BOOL)getShouldShowRectAndIndexPathWithLocation:(CGPoint)location {
    //坐标点的转化
    NSInteger row = (location.y - 20)/44;
    self.sourceRect = CGRectMake(0, row * 44 + 20, [UIScreen mainScreen].bounds.size.width, 44);
    self.selectedPath = [NSIndexPath indexPathForItem:row inSection:0];
    // 如果row越界了,返回NO 不处理peek手势
    NSLog(@"当前所在的行---%zd",self.selectedPath.row);
    return (self.selectedPath.row > 5) ? NO : YES;
}

  实现peek上拉菜单

  //在peek打开的控制器里遵守协议

@interface JYPopViewController ()<UIViewControllerPreviewingDelegate>

  //设置菜单项

//peek上拉菜单
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"确认" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"点击了确认");
    }];
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"取消" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"点击了取消");
    }];
    NSArray *actions =@[action1,action2];
    return actions;
}


转载于:https://my.oschina.net/u/2556411/blog/615532

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值