3D touch 的使用
使用3D touch的时候,你可以选择以静态的方式创建快捷方式(直接在info.plist中添加显示项),也可以创建动态的快捷方式,以动态方式创建的快捷方式,可以改变它的标题,子标题,和按钮图标。github代码下载
1.以静态方式创建
首先在info.plist 文件中添加一下选项:
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemType</key>
<string>com.wsy.share</string>
<key>UIApplicationShortcutItemTitle</key>
<string>Share</string>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeShare</string>
</dict>
</array>
</key>
解释:
UIApplicationShortcutItemType:触发3D Touch时显示的图标类型,这里定义的是UIApplicationShortcutIconTypeShare显示分享的图标,这个快捷方式图标有很多,iOS9.1,苹果有加入了一大部分,当然你也可以自定义自己图标,理解更多可以参考UIApplicationShortcutIcon
UIApplicationShortcutItemTitle:显示的标题,share,可以自定义你自己的标题
UIApplicationShortcutItemType:类型,也就是唯一标示,当你点击后,来判断点击的是哪一个
2.添加动态的快捷方式
static NSString *scanQR = @"scan.qr";
static NSString *myInfo = @"my.info";
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 动态快捷键的个数,如果没有,添加2个
if (application.shortcutItems.count == 0) {
UIApplicationShortcutItem *scanItem = [[UIApplicationShortcutItem alloc] initWithType: scanQR localizedTitle:@"scan QR" localizedSubtitle:@"scan qr to add friend" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay] userInfo:nil];
UIApplicationShortcutItem *myInfoItem = [[UIApplicationShortcutItem alloc] initWithType:myInfo localizedTitle:@"my info message" localizedSubtitle:@"detail my info message" icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"btn_sound_open"] userInfo:nil];
// 设置动态的shortcutItem
application.shortcutItems = @[scanItem, myInfoItem];
}
return YES;
}
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler
{
completionHandler([self handelShortcutItem:shortcutItem]);
}
- (BOOL)handelShortcutItem:(UIApplicationShortcutItem *)shortcutItem
{
if (!shortcutItem.type) {
return NO;
}
if (![shortcutItem.type isKindOfClass:[NSString class]]) {
return NO;
}
return [self isMyShortcutItem:shortcutItem];
}
- (BOOL)isMyShortcutItem:(UIApplicationShortcutItem *)shortcutItem
{
NSArray *shortcutItemTypes = @[@"com.wsy.share", scanQR, myInfo];
BOOL isContain = NO;
if (shortcutItem.type) {
isContain = [shortcutItemTypes containsObject:shortcutItem.type];
}
if (isContain) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:shortcutItem.localizedTitle message:shortcutItem.localizedSubtitle preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleCancel handler:nil]];
[self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}
return isContain;
}
3.peek与pop
当想要预览某一想内容的时候可以长按,来显示将要预览的内容;
代码:
第一步,注册
// 判断3D Touch是否可用
// 当然你可以关闭或打开3D Touch,设置-通用-辅助功能-3D Touch
if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
/**
* 注册用户将要点击的view,来作为soureceView,这里使用 _tableView,当然一个视图控制器
* 可以注册多个view,注册后将返回一个id<UIViewControllerPreviewing>的对象,这个对象
* 的生命周期由系统控制,我们不需要管理
*/
id<UIViewControllerPreviewing> retObj = [self registerForPreviewingWithDelegate:self sourceView:_tableView];
NSLog(@" === %@", retObj);
/**
* 如果你想禁用预览,那么可以调用unregisterForPreviewingWithContext: 来禁用
*/
// [self unregisterForPreviewingWithContext: retObj];
第二步,实现代理
#pragma mark - UIViewControllerPreviewingDelegate
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewControllerToCommit];
[self.navigationController presentViewController:nav animated:YES completion:nil];
}
/**
* 当用户点击我们所注册的视图的时候,将出发这个代理
*
* @param previewingContext 上下文对象,用来管理预览预览
* @param location 用户点击注册view的位置
*
* @return
*/
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:location];
if (!indexPath) {
return nil;
}
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
if (!cell) {
return nil;
}
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
DetailViewController *detailVC = [storyBoard instantiateViewControllerWithIdentifier:@"DetailViewController"];
detailVC.detailTitle = _datas[indexPath.row];
/* The preferredContentSize is used for any container laying out a child view controller.
也就是说设置子视图控制器的preferredContentSize,如果不是子视图控制起,设置这里会导致你所要预览的内容不显示
*/
// detailVC.preferredContentSize = CGSizeMake(300, 200);
CGRect rect = cell.frame;
rect.origin.y = cell.frame.origin.y;
/**
* 设置sourceRect,用户点击的时候产生一种视觉效果,让用户知道可以peek
*/
previewingContext.sourceRect = rect;
return detailVC;
}