3D touch功能的封装
2016-04-09 11:15
549 查看
3Dtouch早就已经可用了,我的项目中也已经加入其中,今天就把我封装的一个3Dtouch功能的类分享出来:
首先是.h文件,也是写了很久了,所以就不再捋思路直接上代码了:
#import <Foundation/Foundation.h>
#import "DHFBase_ViewController.h"
@interface DHFBase_3DTouchManagerViewController :
DHFBase_ViewController<UIViewControllerPreviewingDelegate>
#pragma mark - 检测3D Touch是否可用
/**
* @author weloy, 15-12-02 11:12:39
*
* @brief
检测3d touch在设备上是否可用
*
* @return
*/
+ (BOOL)check3DTouchUsable;
#pragma mark - 动态添加icon上的touch选项
/**
* @author weloy, 15-12-02 11:12:07
*
* @brief
给应用icon添加3dtouch选项
*/
+ (void)applicationAddHomeShortcutItems;
/**
* @author weloy, 15-12-02 11:12:05
*
* @brief
处理用户点击touch item后
*
* @param shortcutItem
被选中的项
*
* @return
*/
+ (BOOL)handledShortcutItem:(UIApplicationShortcutItem *)shortcutItem;
#pragma mark - 注册3D Touch
并实现跳转
- (void)registerFor3DTouchPreview;//注册3D
Touch
/**
* @author weloy, 15-12-02 14:12:04
*
* @brief
子类需要重写的方法
*
* @param responderView
相应的View
*
* @return
需要跳转的ViewController
*/
- (UIViewController *)getViewControllerWithResponderView:(UIView *)responderView;
#pragma mark - PEEK页面添加事件按钮
/**
* @author weloy, 15-12-04 17:12:15
*
* @brief
添加3D Touch peek中得选项按钮
*
* @param titles
标题列表
*/
- (void)addPreviewActionItemsTitlesArray:(NSArray <NSString *> *)titles;
/**
* @author weloy, 15-12-04 17:12:21
*
* @brief
子类需要重写方法
实现点击按钮后相应事件
*
* @param index
被选中的index
*/
- (void)previewActionItemsSelectedIndex:(NSInteger)index;
@end
然后是.m文件:
#import "DHFBase_3DTouchManagerViewController.h"
#import "DHFModel_UserInfoManager.h"
@interface
DHFBase_3DTouchManagerViewController ()
{
NSArray <NSString *> * _peekPreviewTitles;
}
@end
@implementation DHFBase_3DTouchManagerViewController
+ (BOOL)check3DTouchUsable{
if (UIDeviceVersion <
9.0) {
return
NO;
}
BOOL touchAble =
APP_DELEGATE.window.traitCollection.forceTouchCapability
== UIForceTouchCapabilityAvailable;
return touchAble;
}
+ (void)applicationAddHomeShortcutItems{
if ([UIApplication
sharedApplication].shortcutItems.count>0)
{
return;
}
UIApplicationShortcutItem * item1 = [[UIApplicationShortcutItem
alloc] initWithType:@"DHFHouseVisit"
localizedTitle:@"看房"
localizedSubtitle:@"免费,快捷,高效"
icon:[UIApplicationShortcutIcon
iconWithTemplateImageName:@"tweet_selected"]
userInfo:@{@"id":@"DHFHouseVisit",@"type":@"1"}];
UIApplicationShortcutItem * item2 = [[UIApplicationShortcutItem
alloc]
initWithType:@"DHFEntrustedSearch"
localizedTitle:@"找房委托"
localizedSubtitle:@"一对一管家式服务"
icon:[UIApplicationShortcutIcon
iconWithTemplateImageName:@"task_selected"]
userInfo:@{@"id":@"DHFEntrustedSearch",@"type":@"2"}];
[UIApplication
sharedApplication].shortcutItems =
@[item1,item2];
}
+ (BOOL)handledShortcutItem:(UIApplicationShortcutItem *)shortcutItem{
//添加跳转代码
if ([shortcutItem.type
isEqualToString:@"DHFHouseVisit"]) {
[APP_DELEGATE.mRootTabBarController
setSelectedIndex:2];
}
if ([shortcutItem.type
isEqualToString:@"DHFEntrustedSearch"]) {
[APP_DELEGATE.mRootTabBarController
setSelectedIndex:0];
UINavigationController * nav = ((UINavigationController *)APP_DELEGATE.mRootTabBarController.selectedViewController);
[nav popToRootViewControllerAnimated:NO];
[nav pushViewController:[[NSClassFromString(@"DHFMenu_EntrustedHouseSearchViewController")
alloc] init]
animated:YES];
}
return
YES;
}
- (void)viewDidLoad{
[super
viewDidLoad];
}
- (void)registerFor3DTouchPreview{
if ([DHFBase_3DTouchManagerViewController
check3DTouchUsable]) {
[self
registerForPreviewingWithDelegate:self
sourceView:self.view];
}
}
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
viewControllerForLocation:(CGPoint)location{
UIView * responderView = [DHFModel_UserInfoManager
sharedManager].mUserLastTouchView;
// UIView * responderView = [previewingContext.sourceView hitTest:location withEvent:nil];
UIViewController * toViewController;
if ([self
respondsToSelector:@selector(getViewControllerWithResponderView:)]) {
toViewController = [self
getViewControllerWithResponderView:responderView];
}
if (!toViewController) {
return
nil;
}
CGRect sourceRect = [previewingContext.sourceView
convertRect:responderView.frame
fromView:responderView.superview];
previewingContext.sourceRect = sourceRect;
return toViewController;
}
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
commitViewController:(UIViewController *)viewControllerToCommit{
[self.navigationController
pushViewController:viewControllerToCommit
animated:YES];
}
//子类需要重写
- (UIViewController *)getViewControllerWithResponderView:(UIView *)responderView{
return
nil;
}
- (NSArray <id <UIPreviewActionItem>> *)previewActionItems{
NSMutableArray * resultArray = [[NSMutableArray
alloc]initWithCapacity:_peekPreviewTitles.count];
WObj(weakSelf,
self);
[_peekPreviewTitles
enumerateObjectsUsingBlock:^(NSString * obj,
NSUInteger idx, BOOL * stop) {
UIPreviewAction * previewAction = [UIPreviewAction
actionWithTitle:obj
style:UIPreviewActionStyleDefault
handler:^(UIPreviewAction * action,
UIViewController * previewViewController) {
[weakSelf previewActionItemsSelectedIndex:idx];
}];
[resultArray addObject:previewAction];
}];
return resultArray;
}
- (void)addPreviewActionItemsTitlesArray:(NSArray<NSString *> *)titles{
_peekPreviewTitles = [titles
copy];
}
- (void)previewActionItemsSelectedIndex:(NSInteger)index{
}
@end
使用的时候要继承这个类。首先要先注册3Dtouch 在viewDidLoad中写上就好
[self
registerFor3DTouchPreview];//添加3D touch
然后就是子类必须实现的一个方法
- (UIViewController *)getViewControllerWithResponderView:(UIView *)responderView{
if ([responderView.superview
isKindOfClass:[DHFMenu_BtnListView
class]]) {
return [self
clickeFuncBtnWithIndex:responderView.tag
andPush:NO];
}else
if([responderView.superview
isKindOfClass:NSClassFromString(@"DHFCustomView_LeftDetailButton")]){
CGFloat index = ((DHFCustomView_LeftDetailButton *)responderView.superview).mTag;
return [self
detailTitleBtnClickedWithIndex:index
andPush:NO];
}else{
return
nil;
}
}
从这个方法中获取要peek进去的viewController。下一篇简要介绍这个方法
UIView * responderView = [DHFModel_UserInfoManager sharedManager].mUserLastTouchView;
这个类的+方法 如你所见一个就是检测是否可用3Dtouch 其他两个是给应用的icon添加3Dtouch效果
首先是.h文件,也是写了很久了,所以就不再捋思路直接上代码了:
#import <Foundation/Foundation.h>
#import "DHFBase_ViewController.h"
@interface DHFBase_3DTouchManagerViewController :
DHFBase_ViewController<UIViewControllerPreviewingDelegate>
#pragma mark - 检测3D Touch是否可用
/**
* @author weloy, 15-12-02 11:12:39
*
* @brief
检测3d touch在设备上是否可用
*
* @return
*/
+ (BOOL)check3DTouchUsable;
#pragma mark - 动态添加icon上的touch选项
/**
* @author weloy, 15-12-02 11:12:07
*
* @brief
给应用icon添加3dtouch选项
*/
+ (void)applicationAddHomeShortcutItems;
/**
* @author weloy, 15-12-02 11:12:05
*
* @brief
处理用户点击touch item后
*
* @param shortcutItem
被选中的项
*
* @return
*/
+ (BOOL)handledShortcutItem:(UIApplicationShortcutItem *)shortcutItem;
#pragma mark - 注册3D Touch
并实现跳转
- (void)registerFor3DTouchPreview;//注册3D
Touch
/**
* @author weloy, 15-12-02 14:12:04
*
* @brief
子类需要重写的方法
*
* @param responderView
相应的View
*
* @return
需要跳转的ViewController
*/
- (UIViewController *)getViewControllerWithResponderView:(UIView *)responderView;
#pragma mark - PEEK页面添加事件按钮
/**
* @author weloy, 15-12-04 17:12:15
*
* @brief
添加3D Touch peek中得选项按钮
*
* @param titles
标题列表
*/
- (void)addPreviewActionItemsTitlesArray:(NSArray <NSString *> *)titles;
/**
* @author weloy, 15-12-04 17:12:21
*
* @brief
子类需要重写方法
实现点击按钮后相应事件
*
* @param index
被选中的index
*/
- (void)previewActionItemsSelectedIndex:(NSInteger)index;
@end
然后是.m文件:
#import "DHFBase_3DTouchManagerViewController.h"
#import "DHFModel_UserInfoManager.h"
@interface
DHFBase_3DTouchManagerViewController ()
{
NSArray <NSString *> * _peekPreviewTitles;
}
@end
@implementation DHFBase_3DTouchManagerViewController
+ (BOOL)check3DTouchUsable{
if (UIDeviceVersion <
9.0) {
return
NO;
}
BOOL touchAble =
APP_DELEGATE.window.traitCollection.forceTouchCapability
== UIForceTouchCapabilityAvailable;
return touchAble;
}
+ (void)applicationAddHomeShortcutItems{
if ([UIApplication
sharedApplication].shortcutItems.count>0)
{
return;
}
UIApplicationShortcutItem * item1 = [[UIApplicationShortcutItem
alloc] initWithType:@"DHFHouseVisit"
localizedTitle:@"看房"
localizedSubtitle:@"免费,快捷,高效"
icon:[UIApplicationShortcutIcon
iconWithTemplateImageName:@"tweet_selected"]
userInfo:@{@"id":@"DHFHouseVisit",@"type":@"1"}];
UIApplicationShortcutItem * item2 = [[UIApplicationShortcutItem
alloc]
initWithType:@"DHFEntrustedSearch"
localizedTitle:@"找房委托"
localizedSubtitle:@"一对一管家式服务"
icon:[UIApplicationShortcutIcon
iconWithTemplateImageName:@"task_selected"]
userInfo:@{@"id":@"DHFEntrustedSearch",@"type":@"2"}];
[UIApplication
sharedApplication].shortcutItems =
@[item1,item2];
}
+ (BOOL)handledShortcutItem:(UIApplicationShortcutItem *)shortcutItem{
//添加跳转代码
if ([shortcutItem.type
isEqualToString:@"DHFHouseVisit"]) {
[APP_DELEGATE.mRootTabBarController
setSelectedIndex:2];
}
if ([shortcutItem.type
isEqualToString:@"DHFEntrustedSearch"]) {
[APP_DELEGATE.mRootTabBarController
setSelectedIndex:0];
UINavigationController * nav = ((UINavigationController *)APP_DELEGATE.mRootTabBarController.selectedViewController);
[nav popToRootViewControllerAnimated:NO];
[nav pushViewController:[[NSClassFromString(@"DHFMenu_EntrustedHouseSearchViewController")
alloc] init]
animated:YES];
}
return
YES;
}
- (void)viewDidLoad{
[super
viewDidLoad];
}
- (void)registerFor3DTouchPreview{
if ([DHFBase_3DTouchManagerViewController
check3DTouchUsable]) {
[self
registerForPreviewingWithDelegate:self
sourceView:self.view];
}
}
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
viewControllerForLocation:(CGPoint)location{
UIView * responderView = [DHFModel_UserInfoManager
sharedManager].mUserLastTouchView;
// UIView * responderView = [previewingContext.sourceView hitTest:location withEvent:nil];
UIViewController * toViewController;
if ([self
respondsToSelector:@selector(getViewControllerWithResponderView:)]) {
toViewController = [self
getViewControllerWithResponderView:responderView];
}
if (!toViewController) {
return
nil;
}
CGRect sourceRect = [previewingContext.sourceView
convertRect:responderView.frame
fromView:responderView.superview];
previewingContext.sourceRect = sourceRect;
return toViewController;
}
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
commitViewController:(UIViewController *)viewControllerToCommit{
[self.navigationController
pushViewController:viewControllerToCommit
animated:YES];
}
//子类需要重写
- (UIViewController *)getViewControllerWithResponderView:(UIView *)responderView{
return
nil;
}
- (NSArray <id <UIPreviewActionItem>> *)previewActionItems{
NSMutableArray * resultArray = [[NSMutableArray
alloc]initWithCapacity:_peekPreviewTitles.count];
WObj(weakSelf,
self);
[_peekPreviewTitles
enumerateObjectsUsingBlock:^(NSString * obj,
NSUInteger idx, BOOL * stop) {
UIPreviewAction * previewAction = [UIPreviewAction
actionWithTitle:obj
style:UIPreviewActionStyleDefault
handler:^(UIPreviewAction * action,
UIViewController * previewViewController) {
[weakSelf previewActionItemsSelectedIndex:idx];
}];
[resultArray addObject:previewAction];
}];
return resultArray;
}
- (void)addPreviewActionItemsTitlesArray:(NSArray<NSString *> *)titles{
_peekPreviewTitles = [titles
copy];
}
- (void)previewActionItemsSelectedIndex:(NSInteger)index{
}
@end
使用的时候要继承这个类。首先要先注册3Dtouch 在viewDidLoad中写上就好
[self
registerFor3DTouchPreview];//添加3D touch
然后就是子类必须实现的一个方法
- (UIViewController *)getViewControllerWithResponderView:(UIView *)responderView{
if ([responderView.superview
isKindOfClass:[DHFMenu_BtnListView
class]]) {
return [self
clickeFuncBtnWithIndex:responderView.tag
andPush:NO];
}else
if([responderView.superview
isKindOfClass:NSClassFromString(@"DHFCustomView_LeftDetailButton")]){
CGFloat index = ((DHFCustomView_LeftDetailButton *)responderView.superview).mTag;
return [self
detailTitleBtnClickedWithIndex:index
andPush:NO];
}else{
return
nil;
}
}
从这个方法中获取要peek进去的viewController。下一篇简要介绍这个方法
UIView * responderView = [DHFModel_UserInfoManager sharedManager].mUserLastTouchView;
这个类的+方法 如你所见一个就是检测是否可用3Dtouch 其他两个是给应用的icon添加3Dtouch效果
相关文章推荐
- 自己简单封装的一个CDialog类实例
- C# 面向对象三大特性:封装、继承、多态
- PHP类的封装与继承详解
- 理解javascript封装
- 简单理解JavaScript中的封装与继承特性
- javascript封装简单实现方法
- 封装好的一个万能检测表单的方法
- jquery datatable后台封装数据示例代码
- 详解js中class的多种函数封装方法
- c#基础学习之封装
- 使用对象封装ajax重复调用的方法
- Java游戏服务器之数据库表存取封装
- 纯JavaScript实现的兼容各浏览器的添加和移除事件封装
- PHP面向对象三大特点学习(充分理解抽象、封装、继承、多态)
- javascript的函数、创建对象、封装、属性和方法、继承
- 异步的SQL数据库封装详解
- 深入解析C++编程中类的封装特性
- C++封装IATHOOK类实例
- javascript封装 Cookie 应用接口
- WebService 的简单封装接口调用方法