【问题标题】:How to handle multiple touch gesture at a time?如何一次处理多个触摸手势?
【发布时间】:2016-04-25 06:56:35
【问题描述】:

我必须一次在我的imageview 上执行所有手势,这意味着用户可以在双击时一次单击移动imageview,也可以进行缩放和旋转。现在我发现这种方法可以执行多个手势但无法执行缩放和旋转。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

在我被应用 UIPinchGestureRecognizer,UIRotationGestureRecognizerUIPanGestureRecognizer 之前,它一次只能处理一个手势。请帮我解决这个问题,或者如果可能的话给我任何其他更好的选择。

【问题讨论】:

  • 这可能会有所帮助*.com/questions/36787393/…
  • @EI Captain 就我而言 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 方法在任何多点触摸上都没有被调用,当这个方法被调用时,你能建议我吗?
  • 你必须添加手势 [view addGestureRecognizer:longPress];
  • @EI Captain 仍然没有得到我想要的。

标签: ios objective-c uigesturerecognizer multi-touch


【解决方案1】:
UIPinchGestureRecognizer *pinchGesture = ... //initialize
pinchGesture.delegate = self;
//same for rotation gesture
//and then implement delegate method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 

【讨论】:

  • 是的,你是对的。我解决了我的问题,只是忘记设置委托不需要使用上述方法。
【解决方案2】:

这样设置手势。

self.view.multipleTouchEnabled=YES;
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveViewWithGestureRecognizer:)];
panGestureRecognizer.delegate=self;
[self.sub_View addGestureRecognizer:panGestureRecognizer];

UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationWithGestureRecognizer:)];
rotationGestureRecognizer.delegate=self;
[self.sub_View addGestureRecognizer:rotationGestureRecognizer];

UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchWithGestureRecognizer:)];
pinchGestureRecognizer.delegate=self;
[self.sub_View addGestureRecognizer:pinchGestureRecognizer];

[self.imageView setUserInteractionEnabled:YES];

处理动作

 -(void)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer{

[self.superImage_View setAlpha:0.5];
if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded) {
    [self.superImage_View setAlpha:1.0];
}
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
pinchGestureRecognizer.scale =1.0;
 } 

 -(void)handleRotationWithGestureRecognizer:(UIRotationGestureRecognizer *)rotationGestureRecognizer{
[self.superImage_View setAlpha:0.5];
if (rotationGestureRecognizer.state == UIGestureRecognizerStateEnded) {
    [self.superImage_View setAlpha:1.0];
}
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotationGestureRecognizer.rotation);
rotationGestureRecognizer.rotation = 0.0;

}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
  }

-(void)moveViewWithGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer{
//CGPoint touchLocation = [panGestureRecognizer locationInView:self.sub_View];
 // self.imageView.center = touchLocation;
 [self.superImage_View setAlpha:0.5];
CGPoint translation = [panGestureRecognizer translationInView:self.view];
self.imageView.center = CGPointMake(self.imageView.center.x + translation.x,
                                     self.imageView.center.y + translation.y);
[panGestureRecognizer setTranslation:CGPointMake(0, 0) inView:self.view];

if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) {

    CGPoint velocity = [panGestureRecognizer velocityInView:self.view];
    CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
    CGFloat slideMult = magnitude / 200;
  //  NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);

    float slideFactor = 0.1 * slideMult; // Increase for more of a slide
   CGPoint finalPoint = CGPointMake(self.imageView.center.x + (velocity.x * slideFactor),
                                     self.imageView.center.y + (velocity.y * slideFactor));
    finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
    finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);

    [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.imageView.center = finalPoint;
    } completion:nil];
     [self.superImage_View setAlpha:1.0];
}
}

【讨论】:

    最近更新 更多