3Dtouch 基本原理

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

   

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

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

   

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- ( 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、实现代理方法

  遵守协议

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

  //peekpop相关属性


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

  注册

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

  实现peek,pop方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//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打开的控制器里遵守协议

?
1
@interface JYPopViewController ()<UIViewControllerPreviewingDelegate>

  //设置菜单项

?
1
2
3
4
5
6
7
8
9
10
11
12
//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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值