Developer/iOS

[iOS9]3D-Touch-Example

블로blow 2015. 12. 3. 17:13
728x90


3D터치를 하기위해 View에 3d터치 등록.

*.m viewDidLoad
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
    [self registerForPreviewingWithDelegate:self sourceView:self.image1];
    [self registerForPreviewingWithDelegate:self sourceView:self.image2];
}
else {
    NSLog(@"ForceTouch not available. use LongPress...");
}

Head파일에 Delegate 등록.

@interface ViewController : UIViewController <UIViewControllerPreviewingDelegate>


픽을 했을때의 델리게이트. - (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location { DetailImageViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailImageView"]; vc.preferredContentSize = CGSizeMake(0, 300); vc._delegate = self; if(previewingContext.sourceView.tag == 1) { vc.imageTag = 1; }else if(previewingContext.sourceView.tag == 2) { vc.imageTag = 2; } previewingContext.sourceRect = CGRectMake(0, 0, previewingContext.sourceView.frame.size.width, previewingContext.sourceView.frame.size.height); return vc; }

- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    [self presentViewController:viewControllerToCommit animated:YES completion:nil];
}


포스터치를 했을때, 보이는 메뉴 설정.

DetailImageViewController.m

//add previewActionItem
- (NSArray <id <UIPreviewActionItem>> *)previewActionItems
{
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Soccer" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
    [self._delegate callParentControll:TRUE];
    }];
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"BasketBall" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
    [self._delegate callParentControll:FALSE];
    }];
    UIPreviewAction *action3_1 = [UIPreviewAction actionWithTitle:@"BaseBall" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
    [self._delegate callParentControll:FALSE];
    }];
    UIPreviewAction *action3_2 = [UIPreviewAction actionWithTitle:@"VolleyBall" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
    [self._delegate callParentControll:FALSE];
    }];
    UIPreviewActionGroup *action3 = [UIPreviewActionGroup actionGroupWithTitle:@"More" style:UIPreviewActionStyleDestructive actions:@[action3_1, action3_2]];

    return @[action1, action2, action3];
}




ForceTouch 강도 체크.

Source

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *t = touches.anyObject;

    NSLog(@"touchesBegan : %f / %f", t.force, t.maximumPossibleForce);
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *t = touches.anyObject;

    NSLog(@"touchesMoved : %f / %f", t.force, t.maximumPossibleForce);
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *t = touches.anyObject;

    NSLog(@"touchesEnded : %f / %f", t.force, t.maximumPossibleForce);
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *t = touches.anyObject;

    NSLog(@"touchesCancelled : %f / %f", t.force, t.maximumPossibleForce);
}


728x90