您的位置:首页 > 移动开发 > IOS开发

iOS 3Dtouch 开发

2016-02-16 00:00 471 查看
摘要: UIApplicationShortcutItems,peek,pop,peek上拉菜单

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

转载请注明出处

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



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

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



动态菜单是用代码生成,缺点是在第一次打开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

//实现peek和pop手势:
//1、遵守协议 UIViewControllerPreviewingDelegate
//2、注册 [self registerForPreviewingWithDelegate:self sourceView:self.view];
//3、实现代理方法
遵守协议

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

//peek,pop相关属性

@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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: