iOS之3DTouch的使用---很简单,看我就够啦~~
3DTouch是苹果在iOS9之后新推出的功能,功能大致可以分成两种,一种是长按app的icon,会出现以下的界面,还有一种是在app内部的某个视图上使用,效果如下图。 详细的效果也可以参见微信。微信就集成了这些功能,在微信的首页
第一个3DTouch的实现直接在AppDelegate类里面实现就行了,贴代码啦
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self setup3DTouch:application];
return YES;
}
// 3DTouch点击的回调方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
NSLog(@"------%@",shortcutItem);
}
- (void)setup3DTouch:(UIApplication *)application
{
/**
type 该item 唯一标识符
localizedTitle :标题
localizedSubtitle:副标题
icon:icon图标 可以使用系统类型 也可以使用自定义的图片
userInfo:用户信息字典 自定义参数,完成具体功能需求
*/
// UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"标签.png"];
UIApplicationShortcutIcon *cameraIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCompose];
UIApplicationShortcutItem *cameraItem = [[UIApplicationShortcutItem alloc] initWithType:@"typeOne" localizedTitle:@"功能1" localizedSubtitle:@"" icon:cameraIcon userInfo:nil];
UIApplicationShortcutIcon *shareIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
UIApplicationShortcutItem *shareItem = [[UIApplicationShortcutItem alloc] initWithType:@"typeTwo" localizedTitle:@"功能2" localizedSubtitle:@"" icon:shareIcon userInfo:nil];
/** 将items 添加到app图标 */
application.shortcutItems = @[cameraItem,shareItem];
}
关于在app内部的3DTouch效果,可以参见微信的首页。
微信的首页是一个tableview列表,在实现表的协议方法的时候需要在这个方法里面注册
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
注册的方法如下:
// cell注册绑定,设置代理,实现协议
if ([self respondsToSelector:@selector(traitCollection)]) {
if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)]) {
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[self registerForPreviewingWithDelegate:self sourceView:cell];
}
}
}
声明协议 <UIViewControllerPreviewingDelegate>
实现此协议的代理方法
#pragma mark - UIViewControllerPreviewingDelegate
// 3DTouch结束时预览的界面 这里预览的界面是--DetailViewController--
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
NSIndexPath *index = [mainTableView indexPathForCell:(UITableViewCell *)[previewingContext sourceView]];
DetailViewController *VC = [[DetailViewController alloc] init];
VC.viewTitle = [NSString stringWithFormat:@"%ld",(long)index.section];
previewingContext.sourceRect = previewingContext.sourceView.frame;
return VC;
}
// 点击功能模块需要实现的方法
-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self showViewController:viewControllerToCommit sender:self];
}
注意,这里预览的界面是 DetailViewController ,需要在 DetailViewController.m文件中实现下面的方法,这个是预览界面需要实现的点击功能
// 预览界面需要实现的点击功能
- (NSArray<id<UIPreviewActionItem>>*)previewActionItems
{
UIPreviewAction *action0 = [UIPreviewAction actionWithTitle:@"action0" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"%s, line = %d, action0 = %@, previewViewController = %@", __FUNCTION__, __LINE__, action, previewViewController);
}];
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"action1" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"%s, line = %d, action1 = %@, previewViewController = %@", __FUNCTION__, __LINE__, action, previewViewController);
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"action2" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"%s, line = %d, action2 = %@, previewViewController = %@", __FUNCTION__, __LINE__, action, previewViewController);
}];
UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"action3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"%s, line = %d, action2 = %@, previewViewController = %@", __FUNCTION__, __LINE__, action, previewViewController);
}];
//该按钮可以是一个组,点击该组时,跳到组里面的按钮.
UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:@"actionGroup" style:UIPreviewActionStyleSelected actions:@[action2, action3]];
//直接返回数组.
return @[action0,action1,actionGroup];
}
--over--