移动互联网技术分享

iPhone6S 后的 3D - Touch

2016-05-02  本文已影响37人  littleDad
点击app icon 显示3D显示效果
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //动态添加 3Dtouch
    self.window = [UIWindow new];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.frame = [[UIScreen mainScreen] bounds];
    
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];
    self.window.rootViewController = navi;
    [self.window makeKeyAndVisible];
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"one" localizedTitle:@"Little" localizedSubtitle:@"王先森" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay] userInfo:nil];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"boss_red_dot"];

    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"two" localizedTitle:@"Daddy" localizedSubtitle:@"575385842@qq.com" icon:icon2 userInfo:nil];
    [UIApplication sharedApplication].shortcutItems = @[item1, item2];
    
    
    return YES;
}
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    //对应的 3D 添加 效果
    UINavigationController *navi = (UINavigationController *)self.window.rootViewController;
    
    ViewController *vc = [[ViewController alloc] init];
    if ([shortcutItem.type isEqualToString:@"one"]) {

        
        vc.title = @"One";
        vc.view.backgroundColor = [UIColor greenColor];

    } else if ([shortcutItem.type isEqualToString:@"two"]) {
        
        vc.title = @"Two";
        vc.view.backgroundColor = [UIColor orangeColor];

    }
    [navi pushViewController:vc animated:YES];
}
点击UIView触发Touch
- (BOOL)check3DTouch{
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        return YES;
    } else {
        return NO;
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    if ([self check3DTouch]) {
        UITouch *touch = [touches anyObject];
        self.backgroundColor = [UIColor colorWithRed:touch.force / touch.maximumPossibleForce  green:0.0f blue:0.0f alpha:1.0f];
    } else {
        NSLog(@"CAN NOT USE 3D TOUCH!");
    }
}

touch.force对应的是touch 的力度,touch.maximumPossibleForce可感应的最大力度。
check3DTouch判断当前的设备支持3d touch功能与否。

Touch 点击单独的一个UIView,想去让这个UIView里面的子控件做出反应,例UITableVIew 中的cell

_tableview一个继承UITableView通过点击cell触发touch效果

//peek
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    if ([self.presentedViewController isKindOfClass:[ViewController class]]) {
        //已经推出 过了 就 不要再去 使用这个 代理了
        return nil;
    } else {
//        ViewController *vc = [[ViewController alloc] init];
//        vc.title = @"previewVC";
//        vc.view.backgroundColor = [UIColor cyanColor];
        location = [self.view convertPoint:location toView:_tableview];
        NSIndexPath *indexPath = [_tableview indexPathForRowAtPoint:location];

        ViewController *displayVC = [[ViewController alloc] init];
        displayVC.title = [_tableview cellForRowAtIndexPath:indexPath].textLabel.text;
        displayVC.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 256.0
                                                         green:arc4random() % 256 / 256.0
                                                          blue:arc4random() % 256 / 256.0
                                                         alpha:1.0];
        
        // peek预览窗口大小
        displayVC.preferredContentSize = CGSizeMake(0.0, 100 * indexPath.row);
        
        // 进入peek前不被虚化的rect
        previewingContext.sourceRect = [self.view convertRect:[_tableview cellForRowAtIndexPath:indexPath].frame fromView:_tableview];
        
        return displayVC;

    }
}
//pop
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    [self showViewController:viewControllerToCommit sender:self];
}

location = [self.view convertPoint:location toView:_tableview];

NSIndexPath *indexPath = [_tableview indexPathForRowAtPoint:location];

peek 提示框

//peek 提示框
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Little" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"点击执行的操作");
    }];
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"Daddy" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
    }];
 
    NSArray *actions = @[action1, action2];
    
    UIPreviewActionGroup *group = [UIPreviewActionGroup actionGroupWithTitle:@"多组按钮" style:UIPreviewActionStyleDefault actions:actions];
    
    UIPreviewAction *action4 = [UIPreviewAction actionWithTitle:@"单组按钮" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
    }];
    
    NSArray *array = @[group, action4];
    
    return array;
}
上一篇 下一篇

猜你喜欢

热点阅读