- iOS9之后使用3DTouch
- 3DTouch功能主要分为两大块:主屏幕Icon上的Quick Action;
Peek(预览)和Pop(跳至预览的详细界面)
实现 Quick Action
实现方式有两种,一种是在工程文件info.plist里静态设置,另一种是代码的动态实现。
最多只能加4个item,无论使用哪种方法创建
静态方法
动态方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建快捷选项
UIApplicationShortcutItem *item0 = [[UIApplicationShortcutItem alloc]initWithType:@"com.share" localizedTitle:@"分享" localizedSubtitle:@"分享副标题" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare] userInfo:nil];
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"com.other" localizedTitle:@"其他" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"test"] userInfo:nil];
//添加到快捷选项数组
[UIApplication sharedApplication].shortcutItems = @[item0, item1];
// 首次启动App
UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
if (shortcutItem) {
// 根据不同标识执行不同操作
if([shortcutItem.type isEqualToString:@"com.share"]){
} else if ([shortcutItem.type isEqualToString:@"com.other"]) {
}
return NO;
}
return YES;
}
// 如果APP没被杀死,还存在后台,点开Touch会调用该代理方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
if (shortcutItem) {
if([shortcutItem.type isEqualToString:@"com.share"]){
} else if ([shortcutItem.type isEqualToString:@"com.other"]) {
}
}
if (completionHandler) {
completionHandler(YES);
}
}
Peek和Pop的实现
判断是否支持
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
}
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
}
}
1、注册(在哪个页面上使用该功能就注册在哪个页面上)
self registerForPreviewingWithDelegate:selfsourceView:self.mainTableView];
2、遵守UIViewControllerPreviewingDelegate
3、实现代理
//peek(预览)
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
/** 转换坐标 */
CGPoint p = [self.maintTableview convertPoint:CGPointMake(location.x, location.y ) fromView:self.view];
/** 通过坐标活的当前cell indexPath */
NSIndexPath *indexPath = [self.maintTableview indexPathForRowAtPoint:CGPointMake(p.x, p.y + 64)];
/** 获得当前cell */
UITableViewCell *cell = [self.maintTableview cellForRowAtIndexPath:indexPath];
MyPreviewingViewController *peekViewController = [[MyPreviewingViewController alloc] init];
peekViewController.preferredContentSize = CGSizeMake(0.0, 300);
previewingContext.sourceRect = cell.frame;
return peekViewController;
}
//pop(按用点力进入)
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
[self showViewController:viewControllerToCommit sender:self];
}
预览界面上滑弹出框
重写previewActionItems
#pragma mark - Preview Actions
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
// 生成UIPreviewAction
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Action 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Action 1 selected");
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"Action 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Action 2 selected");
}];
UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"Action 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Action 3 selected");
}];
UIPreviewAction *tap1 = [UIPreviewAction actionWithTitle:@"tap 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"tap 1 selected");
}];
UIPreviewAction *tap2 = [UIPreviewAction actionWithTitle:@"tap 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"tap 2 selected");
}];
UIPreviewAction *tap3 = [UIPreviewAction actionWithTitle:@"tap 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"tap 3 selected");
}];
// 塞到UIPreviewActionGroup中
NSArray *actions = @[action1, action2, action3];
NSArray *taps = @[tap1, tap2, tap3];
UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group" style:UIPreviewActionStyleDefault actions:actions];
UIPreviewActionGroup *group2 = [UIPreviewActionGroup actionGroupWithTitle:@"Tips Group" style:UIPreviewActionStyleDestructive actions:taps];
NSArray *group = @[group1,group2];
return group;
}