分享

3D Touch魔性入门(一)

 叹落花 2015-12-17


Part1

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    //这是一枚可变的icon
    UIMutableApplicationShortcutItem *shortItem1 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"1" localizedTitle:@"对!没错!"];
    //这是icon的图片,对图片有要求哦,不符合规则显示不来哦
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"Image1"];
    //给icon设置一下图片
    shortItem1.icon = icon1;

    UIMutableApplicationShortcutItem *shortItem2 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"2" localizedTitle:@"我就是"];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"Image2"];
    shortItem2.icon = icon2;

    UIMutableApplicationShortcutItem *shortItem3 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"3" localizedTitle:@"一台完美的"];
    UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"Image3"];
    shortItem3.icon = icon3;

    UIMutableApplicationShortcutItem *shortItem4 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"4" localizedTitle:[PhoneInfoManager getCurrentDeviceModel]];
    UIApplicationShortcutIcon *icon4 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"Image4"];
    shortItem4.icon = icon4;

    NSArray *shortItems = [[NSArray alloc] initWithObjects:shortItem4, shortItem3, shortItem2, shortItem1, nil];
    NSLog(@"%@", shortItems);
    [[UIApplication sharedApplication] setShortcutItems:shortItems];
    return YES;
}

这就是效果!

在AppDelegate里边实现这一段代码就可以响应点击的icon的事件(可以根据shortcutItem.type判断哪个icon)

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
    //很普通的一枚Alert(Xcode7废弃了UIAlertVIew好不习惯啊)
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"讨厌,你又点了人家" message:@"你要对我负责!!!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"我会的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"我一定会的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}

Part2

接下来就是稍微比较复杂的用3DTouch控制ViewController等UI。


轻按效果图

重按效果图

咱们先去你想要实现3DTouch的类里注册实现UIViewControllerPreviewingDelegate

@interface ViewController ()<UIViewControllerPreviewingDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    /**
     *  如果支持3DTouch,就添加3DTouch的代理
     */
   if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
    [self registerForPreviewingWithDelegate:self sourceView:self.view];
   }

    _lbl.text = [NSString stringWithFormat:@"****妈妈再也不用担心我装逼了****\n\n我的设备: %@\n\n我的内存: %.2f MB\n\n我的储空间: %qi GB\n\n********************************",[PhoneInfoManager getCurrentDeviceModel],[PhoneInfoManager logMemoryInfo],[PhoneInfoManager freeDiskSpaceInBytes]];
}

然后使劲按,就会触发到事件

- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)context viewControllerForLocation:(CGPoint) point
{
    /**
     轻按

     - returns: 要显示的VC
     */
    BaseViewController *vc = [[BaseViewController alloc] init];
    vc.view.frame = self.view.frame;

    UILabel *lbl = [[UILabel alloc]initWithFrame:vc.view.frame];
    lbl.textColor = [UIColor whiteColor];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.numberOfLines = 0;
    lbl.font = [UIFont systemFontOfSize:50.0];
    lbl.text = [NSString stringWithFormat:@"不要这么使劲的戳人家嘛\n\n?(? ?·?ω?·? ?)?\n\n淫家只是一台可以摸的iPhone了啦"];
    vc.view = lbl;

    /**
     *  轻按显示VC大小范围
     *
     *  @param 0.0f   显示宽度(0为不限制?)
     *  @param 450.0f 显示高度
     *
     *  @return vc
     */
    vc.preferredContentSize = CGSizeMake(0.0f,300.0f);

    /**
     *  触摸和轻按中间的过度模糊层(rect为0就没有这个效果啦!!!系统会去掉,设为float最小值会全覆盖)
     *
     *  @param CGFLOAT_MIN float最小值
     *  @param CGFLOAT_MIN float最小值
     *  @param CGFLOAT_MIN float最小值
     *  @param CGFLOAT_MIN float最小值
     *
     *  @return 感觉像没用
     */
    CGRect rect = CGRectMake(CGFLOAT_MIN, CGFLOAT_MIN, CGFLOAT_MIN ,CGFLOAT_MIN);
    context.sourceRect = rect;

    return vc;
}

-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{

    /**
     *  重按push进去,然后3秒后移除
     */
    [self showViewController:viewControllerToCommit sender:self];

    double delayInSeconds = 3.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [viewControllerToCommit dismissViewControllerAnimated:YES completion:^{
        }];
    });
}

然后你就会问了,呢个BaseViewController是干吗用的,因为3DTouch轻按时候上拉事件,必须重写将要弹出ViewController的一个方法。


轻按上划触发事件效果图
#import "BaseViewController.h"

@interface BaseViewController ()<UIViewControllerPreviewingDelegate>

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//预览页面 底部Action Items
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
    UIPreviewAction *p1 =[UIPreviewAction actionWithTitle:@"不要酱紫" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
    }];

    UIPreviewAction *p2 =[UIPreviewAction actionWithTitle:@"好了啦" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
    }];

    NSArray *actions = @[p1,p2];
    return actions;
}

我是代码:?????https://github.com/JamesGu0116/3DTouchDoge

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多