废话不多说,直接上干货!
应用外的3D Touch效果
- UIApplicationShortcutItems:快捷项对象,即3D Touch的一个相应按钮。
- UIApplicationShortcutItemType:快捷项的类型,字符串类型,可以用来区分具体操作。
- UIApplicationShortcutItemTitle :快捷项的主标题,类似于UITableViewCell的title属性。
- UIApplicationShortcutItemSubtitle:快捷项的子标题,如果设置了会和主标题一同展示,类似于UITableView的subTitle属性。
- UIApplicationShortcutItemIconType:系统默认提供的快捷项图标。
- UIApplicationShortcutItemIconFile:使用自己的图标,将图片路径传入即可。注意图片尺寸是35*35。
- UIApplicationShortcutItemUserInfo:字典类型,包含应用程序的版本信息、程序启动或者更新注意事项。
应用外的3D Touch效果有plist文件和代码两种实现方式,下面逐一介绍两种实现方式的具体做法。
-
plist文件:
-
代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//使用自己的图片,无子标题
UIApplicationShortcutIcon *firstItemIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"Chat"];
UIMutableApplicationShortcutItem *firstItem = [[UIMutableApplicationShortcutItem alloc]initWithType:@"myFirst" localizedTitle:@"添加" localizedSubtitle:nil icon:firstItemIcon userInfo:nil];
//使用系统提供的图片
UIApplicationShortcutIcon *secondItemIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
UIMutableApplicationShortcutItem *secondItem = [[UIMutableApplicationShortcutItem alloc]initWithType:@"mySecond" localizedTitle:@"分享" localizedSubtitle:@"fenxiang" icon:secondItemIcon userInfo:nil];
application.shortcutItems = @[firstItem,secondItem];
return YES;
}
上面分别陈述了两种应用外3D Touch效果的实现方式,下面介绍几点实现过程中的注意事项:
- 两种方式是可以结合使用的,plist属于静态配置,代码是属于动态配置。我们再实现时,可以将确定不变的快捷项在plist文件中配置,将不确定的的AppDelegate中用代码实现。
- 虽然两种方式是可以结合使用的,但是最多只允许添加4个快捷项,先添加plist中配置的。也就是说,plist中配置了4个快捷项,那么代码添加的快捷项是无效的。
- 配置的快捷项是放在数组中的,快捷项展示也是按照数组顺序由下自上展示的。
响应事件
添加快捷项,在AppDelegate.m文件中实现响应事件,我们可以通过以下的几种方式区分点击的快捷项:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIApplicationShortcutIcon *firstItemIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"Chat"];
UIMutableApplicationShortcutItem *firstItem = [[UIMutableApplicationShortcutItem alloc]initWithType:@"myFirst" localizedTitle:@"添加" localizedSubtitle:@"tianjia" icon:firstItemIcon userInfo:nil];
UIApplicationShortcutIcon *secondItemIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
UIMutableApplicationShortcutItem *secondItem = [[UIMutableApplicationShortcutItem alloc]initWithType:@"mySecond" localizedTitle:@"分享" localizedSubtitle:@"fenxiang" icon:secondItemIcon userInfo:nil];
application.shortcutItems = @[firstItem,secondItem];
return YES;
}
- 按照类型判断操作
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
if ([shortcutItem.type isEqualToString:@"myFirst"]) {
NSLog(@"执行添加事件");
}else if ([shortcutItem.type isEqualToString:@"mySecond"]) {
NSLog(@"执行分享的操作");
}
}
- 按照标题判断操作
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
if ([shortcutItem.localizedTitle isEqualToString:@"添加"]) {
NSLog(@"执行添加事件");
}else if ([shortcutItem.localizedTitle isEqualToString:@"分享"]) {
NSLog(@"执行分享的操作");
}
}
- 按照子标题判断操作
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
if ([shortcutItem.localizedSubtitle isEqualToString:@"tianjia"]) {
NSLog(@"执行添加事件");
}else if ([shortcutItem.localizedSubtitle isEqualToString:@"fenxiang"]) {
NSLog(@"执行分享的操作");
}
}
应用内3D Touch效果
应用内3D Touch分为peek检测、peek和pop三个阶段,每个阶段的按压力度也是逐渐增大的,首先需要遵守UIViewControllerPreviewingDelegate协议,下面对三个阶段分别进行代码实现:
实例代码是我自己封装的一个图片选择器。
- peek检测
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PhotoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCollectionCell" forIndexPath:indexPath];
// 在资源的集合中获取第一个集合,并获取其中的图片
PHAsset *asset = self.album[indexPath.item];
cell.photoImageView.image = [self.photoAPI getImageWithAsset:asset targetSize:_targetSize synchronous:YES completionHandler:nil];
cell.asset = asset;
if (self.selectedArr.count > 0) {
cell.selectBtn.selected = [_selectedArr containsObject:cell.asset] ? YES : NO;
}else {
cell.selectBtn.selected = NO;
}
//检测3D Touch的可用性,如果可用,将self设置为代理,按压cell响应3D Touch效果。
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[self registerForPreviewingWithDelegate:self sourceView:cell];
}
return cell;
}
- peek
#pragma mark - UIViewControllerPreviewingDelegate
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
//坐标转换,将手指按在屏幕上的坐标转换成为collectionView的坐标。
CGPoint cellLocation = [self.photoCollectionView convertPoint:location fromView:[previewingContext sourceView]];
//根据触摸点获取按压的cell
NSIndexPath *cellIndex = [self.photoCollectionView indexPathForItemAtPoint:cellLocation];
PhotoCollectionCell *cell = (PhotoCollectionCell *)[self.photoCollectionView cellForItemAtIndexPath:cellIndex];
//返回一个VC,被peek的控制器
MXBrowserPhotoController *browser = [[MXBrowserPhotoController alloc] initWithSelectedAssets:@[cell.asset] pageControl:NO];
return browser;
}
peek预览阶段可以上滑显示更多操作,操作项添加方法在返回的MXBrowserPhotoController.m文件中实现。
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
UIPreviewAction *like = [UIPreviewAction actionWithTitle:@"喜欢" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"点击了喜欢");
}];
UIPreviewAction *selec = [UIPreviewAction actionWithTitle:@"选中" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"点击选中");
}];
UIPreviewAction *cancle = [UIPreviewAction actionWithTitle:@"取消" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"点击取消");
}];
return @[like, selec, cancle];
}
- pop
#pragma mark - UIViewControllerPreviewingDelegate
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
//pop阶段展示的VC
[self presentViewController:viewControllerToCommit animated:YES completion:nil];
}