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

iOS9 3D Touch 使用教程

2016-10-20 15:29 375 查看

1应用图标 3D Touch

3D Touch 分为重压和轻压手势, 分别称作POP(第一段重压)和PEEK(第二段重压), 外面的图标只需要POP即可.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//使用系统自带图标
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];

UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];

//使用自己的图片
UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"自己的图片"];

/** type 该item 唯一标识符
localizedTitle :标题
localizedSubtitle:副标题
icon:icon图标 可以使用系统类型 也可以使用自定义的图片
userInfo:用户信息字典 自定义参数,完成具体功能需求
*/
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"item1" localizedTitle:@"标题1" localizedSubtitle:@"sub" icon:icon1 userInfo:nil];

UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"item2" localizedTitle:@"标题2" localizedSubtitle:nil icon:icon2 userInfo:nil];

UIApplicationShortcutItem *item3 = [[UIApplicationShortcutItem alloc]initWithType:@"item3" localizedTitle:@"标题3" localizedSubtitle:nil icon:icon3 userInfo:nil];

NSArray *array = @[item1,item2,item3];
[UIApplication sharedApplication].shortcutItems = array;

return YES;
}

#pragma mark - 3DTouch触发的方法
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
//这里可以实现界面跳转等方法
if ([shortcutItem.type isEqualToString:@"item1"]) {
// SFSafariViewController 属于Safari框架(SafariServices.framework)
SFSafariViewController *sv = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:@"http://weibo.com/p/1005055844400745/home?from=page_100505_profile&wvr=6&mod=data&is_all=1#place"]];
_window.rootViewController = sv;
NSLog(@"按压了第一个标题");
}
else if ([shortcutItem.type isEqualToString:@"item2"])
{
ViewController *vc = [[ViewController alloc]init];
_window.rootViewController = vc;
NSLog(@"按压了第二个标题");
} else if ([shortcutItem.type isEqualToString:@"item3"]) {
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
secondViewController *send = [[secondViewController alloc] init];
[nav pushViewController:send animated:NO];
}
}


//判断是否支持3dtouch,设置委托代理
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
//注册代理
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}

// 代理方法
-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
//location就是重压点坐标,如果按压点在label上执行以下方法
if (location.x > 100 && location.x < 200 && location.y > 100 && location.y < 200) {
SFSafariViewController *mySV = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:URL]];
/****
重要
****/
//第一次按压时,弹出的窗口尺寸,再次按压则跳转到mySV
//当尺寸为CGSizeMake(0, 0);时会有一个默认的尺寸
mySV.preferredContentSize = CGSizeMake(0, 0);
//设置高亮区域
previewingContext.sourceRect = _weiboLabel.frame;
return mySV;
}
return nil;
}

-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
//固定这么写
[self showViewController:viewControllerToCommit sender:self];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  3dtouch