关于3D Touch的教程网上已经很多了,总要写点不一样的地方。本文主要是实习类似于微信带标题的PreviewingView。效果如下:
代码地址(Objective-C & Swift):https://github.com/Xigtun/3DTouchdemo.git
思路大体如下:
1、轻按,在PreviewingView展示时,加上titleLabel,并使目标view向下偏移。
2、重按,进入目标控制器,移除titleLabel,并使目标view的frame恢复正常。
其他细节:有些应用没有开启的时候(被kill掉),在桌面选择quickAction仍能进入正常的页面,有些则是启动到首页。前者是在AppDelegate获取到rootViewController,后者可能就如本例中发送通知了。
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
//亦可在此获取rootViewController进行操作
[[NSNotificationCenter defaultCenter] postNotificationName:@"shortcutItemTouched" object:shortcutItem.type];
}
主要代码如下:
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self setTouchShortcut];
return YES;
}
- (void)setTouchShortcut
{
UIApplicationShortcutItem *firstItem = [[UIApplicationShortcutItem alloc] initWithType:@"Red" localizedTitle:@"Red Title" localizedSubtitle:@"Red Descibe" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeDate] userInfo:nil];
UIApplicationShortcutItem *secondItem = [[UIApplicationShortcutItem alloc] initWithType:@"Yellow" localizedTitle:@"Yellow Title" localizedSubtitle:@"Yellow Describe" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeHome] userInfo:nil];
UIApplicationShortcutItem *thirdItem = [[UIApplicationShortcutItem alloc] initWithType:@"Blue" localizedTitle:@"Blue Title" localizedSubtitle:@"Blue Describe" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLove] userInfo:@{@"name":@"haha"}];
//haha
NSArray *items = @[firstItem, secondItem, thirdItem];
[UIApplication sharedApplication].shortcutItems = items;
}
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"shortcutItemTouched" object:shortcutItem.type];
}
ViewController:
#pragma mark - UIViewControllerPreviewingDelegate
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSString *controllerTitle = self.cellTitles[indexPath.row];
DetailViewController *detailController = [[DetailViewController alloc] initWithIdentifier:controllerTitle];
previewingContext.sourceRect = cell.frame;
UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(detailController.view.frame), 35)];
titleView.text = controllerTitle;
titleView.textAlignment = NSTextAlignmentCenter;
titleView.backgroundColor = [UIColor whiteColor];
titleView.tag = 1234;
[detailController.view addSubview:titleView];
return detailController;
}
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
for (UILabel *label in viewControllerToCommit.view.subviews) {
if (label.tag == 1234) {
[label removeFromSuperview];
}
}
[self showViewController:viewControllerToCommit sender:self];
}
DetailViewController:
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
UIPreviewAction *actionOne = [UIPreviewAction actionWithTitle:@"Action One" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
}];
UIPreviewAction *actionTwo = [UIPreviewAction actionWithTitle:@"Action Two" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
}];
UIPreviewAction *actionThree = [UIPreviewAction actionWithTitle:@"Action Three" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
}];
return @[actionOne, actionTwo, actionThree];
}