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;
}
發佈了41 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章