3D Touch
2016-01-29 本文已影响584人
徐老茂
去年6s上市之后推出了3DTouch功能,现在很多app都运用了次功能,我对这个功能一直很感兴趣,可是之前一直没钱买(穷啊),现在攒钱买了部8手6s后马上去感受了一下3DTouch,如图.
3Dtouch可以看见现在主流app都拥有这个功能,这个功能确实是简洁一些.
那么看看代码吧.
在AppDelegate里面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//3D Touch 分为重压和轻压手势, 分别称作POP(第一段重压)和PEEK(第二段重压), 外面的图标只需要POP即可.
//POP手势图标初始化
//使用系统自带图标
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
//使用自己的图片
// UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"自己的图片"];
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"item1" localizedTitle:@"标题1" localizedSubtitle:nil icon:icon1 userInfo:nil];
UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"item2" localizedTitle:@"标题2" localizedSubtitle:nil icon:icon2 userInfo:nil];
NSArray *array = @[item1,item2];
[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 *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(@"按压了第二个标题");
}
}
点击第一个标题进入safari浏览器打开的网页,点击第二个进入ViewController.效果图如下:
在Viewcontroller里面我放了一个label,使其也能实现3DTouch效果,看看代码.
Viewcontroller.m
#import "ViewController.h"
#import <SafariServices/SafariServices.h>
#define URL @"http://weibo.com/p/1005055844400745/home?from=page_100505_profile&wvr=6&mod=data&is_all=1#place"
@interface ViewController ()<UIViewControllerPreviewingDelegate>
@property(nonatomic, strong)UILabel *weiboLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}
-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
//固定这么写
[self showViewController:viewControllerToCommit sender:self];
}
-(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
mySV.preferredContentSize = CGSizeMake(0, 400);
previewingContext.sourceRect = _weiboLabel.frame;//设置高亮区域
return mySV;
}
return nil;
}
-(void)loadView
{
[super loadView];
_weiboLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
_weiboLabel.text = @"我的微博";
[self.view addSubview:_weiboLabel];
//注册代理
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}
大家看看效果图:
这里第一次按压先弹出窗口,如果松手则关闭窗口,如果再次按压才会跳转到浏览器界面.
代码还有不完善的地方,大家可以改改.
好了,今天就到这里,祝大家开心😄