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

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效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS9 3D touch 封装