2016-04-25 25 views
0

我有一次對我imageview執行所有的手勢,意味着用戶可以在上雙擊一個時間上的一個水龍頭移動imageview還縮放和旋轉possible.Now我我發現這個方法執行多個動作,但無法執行縮放和旋轉。如何同時處理多點觸摸手勢?

- (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 

之前,我正在申請UIPinchGestureRecognizerUIRotationGestureRecognizerUIPanGestureRecognizer但它只能處理一個手勢在time.Please幫我解決這個或者如果可能的話給我任何其他更好的選擇。

+0

可能,這將有助於http://stackoverflow.com/questions/36787393/need-to-apply-uirotationgesturerecognizer-followed-by-uilongpressgesturerecongni –

+0

@EI隊長在我的情況 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer方法沒有得到所謂的任意多個觸摸ü可以建議我當這個方法調用得到? –

+0

你必須添加手勢[查看addGestureRecognizer:長按]。 –

回答

0
UIPinchGestureRecognizer *pinchGesture = ... //initialize 
pinchGesture.delegate = self; 
//same for rotation gesture 
//and then implement delegate method 
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
+0

是的,你是對的。我解決了我的問題,只忘記設置委託,不需要使用上述方法。 –

0

設置像這樣的手勢。

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]; 
} 
}