3DTouch功能是iphone的专属功能,我们在开发中用到的几率很大,本篇文章讲解的是应用图标按压的3DTouch功能和界面某个控件按压的3DTouch功能,我的邮箱是KenenCS@163.com,欢迎多多交流!
一.应用图标的3DTouch功能开发
先来一张效果图:
看一下具体代码:
AppDelegate.h
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 32 33 34 35 36 37 38 39 40 41 42 43
| - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self set3DTouch]; return YES; } #pragma mark-------设置APP图标的3DTouch</span> - (void)set3DTouch { UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeHome]; UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLove]; 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; } #pragma mark-----APP图标的3DTouch代理方法 - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler { if ([shortcutItem.type isEqualToString:@"item1"]) { NSLog(@"按压了第一个标题"); }else if ([shortcutItem.type isEqualToString:@"item2"]) { NSLog(@"按压了第二个标题"); } }
|
二.界面按压3DTouch功能
再来看一张效果图:
看一下具体代码:
首先导入SafariServices.framework这个框架,看这个名字你应该就知道是和Safari浏览器有关的,没错,你想的很对!
ViewController.m
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #import <SafariServices/SafariServices.h> @interface ViewController ()<UIViewControllerPreviewingDelegate> { UILabel *label; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; } -(void)loadView { [super loadView]; label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; label.text = @"我的微博"; label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:label]; [self registerForPreviewingWithDelegate:self sourceView:self.view]; } #pragma mark----PreviewingDelegate -(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit { [self showViewController:viewControllerToCommit sender:self]; } -(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location { if (location.x > 100 && location.x < 200 && location.y > 100 && location.y < 200) { SFSafariViewController *mySV = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]; mySV.preferredContentSize = CGSizeMake(0, 400); previewingContext.sourceRect = label.frame; return mySV; } return nil; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
|
三.项目地址
此小demo的地址在GitHub的 3DTouch,欢迎下载交流!