API介紹
UITouch類里API的變化
iOS9中添加的屬性
altitudeAngle
- 當筆平行于平面時,該值為0
- 當筆垂直于平面時,該值為Pi / 2
estimatedProperties
- 當前觸摸對象估計的觸摸特性,返回值是UITouchPropertyies
updatedProperties
- 當前觸摸對象已經(jīng)更新的觸摸特性,返回值是UITouchPropertyies
estimationUpdateIndex
- 當每個觸摸對象的觸摸特性發(fā)生變化時,該值將會單獨增加,返回值是NSNumber
iOS9中添加的方法
- PreciseLocationInView:當前觸摸對象的坐標
- PrecisePreviousLocationInView:當前觸摸對象的前置坐標
- azimuthAngleInview:沿著x軸正向的方位角,當與x軸正向方向相同時,該值為0;當view參數(shù)為nil時尔崔,默認為keyWindow
- azimuthUnitVectorInView:當前觸摸對象的方向上的單位向量
當view參數(shù)為nil時角撞,默認為keyWindow
UIForceTouchCapability
- UIForceTouchCapabilityUnknown 不能確定是否支持壓力感應
- UIForceTouchCapabilityUnavailable 不能支持壓力感應
- UIForceTouchCapabilityAvailable 可以支持壓力感應
UITouchType
- UITouchTypeDirect 垂直的觸摸類型
- UITouchTypeIndirect 非初值的觸摸類型
- UITouchTypeStylus 水平的觸摸類型
關于重按App icon展開菜單的實現(xiàn)
在項目的Appdelegate中找到如下方法呛伴,創(chuàng)建展開菜單
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
for example
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
UIViewController* vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
self.window.rootViewController = vc;
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"item1" localizedTitle:@"我的消息" localizedSubtitle:nil icon:nil userInfo:nil];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"item2" localizedTitle:@"發(fā)布動態(tài)" localizedSubtitle:nil icon:icon2 userInfo:nil];
UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"ico_discovery"];
UIApplicationShortcutItem *item3= [[UIApplicationShortcutItem alloc] initWithType:@"item3" localizedTitle:@"搜索尋味師" localizedSubtitle:nil icon:icon3 userInfo:nil];
application.shortcutItems = @[item3,item2,item1];
}
創(chuàng)建完之后就運行項目,看到效果啦谒所,如圖
之后可以在application:performActionForShortcutItem:completionHandler
方法內(nèi)根據(jù)shortcutItem.type
處理點擊事件
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
for example
if ([shortcutItem.type isEqualToString:@"item1"]) {
MyMessageViewController *vc = [[MyMessageViewController alloc] init];
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController.tabBarController.selectedViewController pushViewController:vc animated:YES];
}
}
關于peek 和 pop的實現(xiàn)
效果圖
首先創(chuàng)建2個viewController,例如HomeViewController
和DetailPageViewController
- HomeViewController:需要添加peek和pop的ViewController
- DetailPageViewController:要展示的View Controller
在HomeViewController中添加<UIViewControllerPreviewingDelegate>,并注冊delegate
[self registerForPreviewingWithDelegate:self sourceView:self.view];
實現(xiàn)該Delegate的方法
-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)context viewControllerForLocation:(CGPoint)location
{
CGPoint point = [self.view convertPoint:location toView:self.tableView];//根據(jù)loaction獲取其在不同坐標系中的點坐標热康,后面會通過這個取獲取數(shù)據(jù)的indexPath
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
CGRect cellRect = cell.frame;
context.sourceRect = cellRect;
Humors *humor = [(FinderInfo*)dataList[indexPath.row] humors][0];//這是我們項目的一個model,大家可以換成自己的model進行操作
DetailPageViewController *controller = [[DetailPageViewController alloc]initWithFlavorId:[humor.param intValue]];//創(chuàng)建需要pop的ViewController
controller.view.frame = self.view.frame;
return controller;
}
-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self showViewController:viewControllerToCommit sender:self];
}
效果圖
然后在DetailPageViewController中實現(xiàn)peek操作
打開前面pop出的ViewController(DetailPageViewController)劣领,在實現(xiàn)文件中添加如下代碼
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
__weak typeof(self) weakSelf = self;
NSString *actionTitle = mUserRes.isAttention?@"取消關注":@"+關注";
UIPreviewAction *p1 =[UIPreviewAction actionWithTitle:actionTitle style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
[weakSelf attention]; //這里添加你需要點擊后的操作
}];
UIPreviewAction *p2 =[UIPreviewAction actionWithTitle:@"取消" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
//這里添加你需要點擊后的操作
}];
NSArray *actions = @[p1,p2];
return actions;
}
Tips:判斷設備是否支持3D touch姐军,需要做判斷
- self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable
- [[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0f
必須要兩樣同時滿足,才可以使用尖淘,不作判斷會引起不支持3D Touch的手機編譯crash
部分內(nèi)容摘自簡書原地址點我