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
//实现peek和pop手势:
//1、遵守协议 UIViewControllerPreviewingDelegate
//2、注册 [self registerForPreviewingWithDelegate:self sourceView:self.view];
//3、实现代理方法
遵守协议
1
|
@interface JYMoreViewController ()<UITableViewDelegate,UITableViewDataSource,UIViewControllerPreviewingDelegate>
|
//peek,pop相关属性
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;
}
|