iPhone 6S 이후의 3D - 터치

6222 단어
앱 아이콘을 클릭하여 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:@"[email protected]" 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];
}

  • shortcutItem.type

  • 터치를 트리거하려면 UIView를 클릭하십시오.
    - (BOOL)check3DTouch{
        if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
            return YES;
        } else {
            return NO;
        }
    }
    
    - (void)touchesMoved:(NSSet *)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.maximumPossibleForce는 감지할 수 있는 최대 힘입니다. check3DTouch 현재 기기가 3d 터치 기능을 지원하는지 확인합니다.

    별도의 UIView를 터치하고 UITableView의 셀과 같이 이 UIView의 자식 컨트롤이 반응하도록 하려면


    _tableview 상속된 UITableView는 셀을 클릭하여 터치 효과를 트리거합니다.

  • 현재 컨트롤러가 UIViewControllerPreviewingDelegate 프로토콜을 따릅니다. 내부에 peek(터치 아래 보기 표시) 및 pop(터치 입력 vc) 메서드를 구현합니다.

  • //peek
    - (UIViewController *)previewingContext:(id)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)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
        [self showViewController:viewControllerToCommit sender:self];
    }
    
    location = [self.view convertPoint:location toView:_tableview];
  • _tableview에서 self.view 지점의 위치를 ​​클릭하십시오.

  • NSIndexPath *indexPath = [_tableview indexPathForRowAtPoint:location];
  • 클릭된 셀 아래 첨자 가져오기

  • 프롬프트 상자 엿보기


    //peek 提示框
    - (NSArray> *)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;
    }
    

    좋은 웹페이지 즐겨찾기