您的位置:首页 > 移动开发 > IOS开发

iOS-3D touch相关开发

2016-03-23 19:05 656 查看
3D Touch,就是屏幕可感应不同的感压力度触控,有Peek Pop 两种新手势。
6s出来的时候,我对这增加的新功能激动不已 ~ ~
想着新技术,新玩意,App会越来越好的 ~ ~


3D Touch目前实现的功能,该实现的也实现了。现在把之前自己写项目的相关代码检出来供大家参考。

写在前面

是从iOS 9之后的系统支持的,UIViewController的.h文件中是这样地:

// Registers a view controller to participate with 3D Touch preview (peek) and commit (pop).
- (id <UIViewControllerPreviewing>)registerForPreviewingWithDelegate:(id<UIViewControllerPreviewingDelegate>)delegate sourceView:(UIView *)sourceView NS_AVAILABLE_IOS(9_0);
- (void)unregisterForPreviewingWithContext:(id <UIViewControllerPreviewing>)previewing NS_AVAILABLE_IOS(9_0);


so,注册前判断下系统还是相当有必要的,判断的宏定义我也给搞出来,哈哈

#define SC_IOS9 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9)

if (SC_IOS9)
{
//注册
if (SC_IOS9)
{
//注册
[self registerForPreviewingWithDelegate:self sourceView:mealsHistoryTableView];;
}


第一弹: 快捷图标进入App

第一步:给快捷入口增加两个item

if (SC_IOS9)
{
//注册
[self threeDTouchForQuickOpenTheAppWithApplication:application];
}


#pragma mark - 3D图标快捷功能进入
/**
*  3Dtouch快捷进入图标
*
*  @param application 当前程序
*/
-(void)threeDTouchForQuickOpenTheAppWithApplication:(UIApplication *)application{
//第一个设置图标img
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"chatShowCal"];
//创建第一个快捷入口
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:noti_3dtouch_chatWithShowCal localizedTitle:@"咨询瘦咖" localizedSubtitle:nil icon:icon1 userInfo:nil];

UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"camera"];
UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:noti_3dtouch_takePhotoOfFood localizedTitle:@"拍照上传" localizedSubtitle:nil icon:icon2 userInfo:nil];
//添加进去
application.shortcutItems = @[item1,item2];
}


第二步:响应该快捷入口,苹果提供的接收方法是:


// Called when the user activates your application by selecting a shortcut on the home screen,
// except when -application:willFinishLaunchingWithOptions: or -application:didFinishLaunchingWithOptions returns NO.
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;


实现该方法 :

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem
completionHandler:(void(^)(BOOL succeeded))completionHandler
{
//判断唯一标示 确定相对应的按钮
if([shortcutItem.type isEqualToString:noti_3dtouch_chatWithShowCal]){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:noti_3dtouch_chatWithShowCal object:nil];
});

}

//判断唯一标示 确定相对应的按钮
if([shortcutItem.type isEqualToString:noti_3dtouch_takePhotoOfFood]){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:noti_3dtouch_takePhotoOfFood object:nil];
});

}
}


OK,你可以搞下来试试了。效果如下,估计想也想到是什么效果了。。



第二弹:TableView实现Cell上的展示预览(peek)

,,随后见,饿的不行了,下班回家吃饭。

a4e0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息