How to handle multiple touch gesture at a time?
Asked Answered
W

2

0

I have to perform all the gesture on my imageview at a time, means user can move imageview on single tap at a time on double tap also zooming and rotation possible.Now i am found this methods to perform multiple gesture but unable to perform zoom and roation.

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

Before i am applied UIPinchGestureRecognizer,UIRotationGestureRecognizer and UIPanGestureRecognizer but it handle only one gesture at a time.Please help me to solve this or give me any other better option if possible.

Welch answered 25/4, 2016 at 6:56 Comment(4)
may be this would help #36787893Parhe
@EI Captain In my case - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer method not getting called on any multiple touch can u suggest me when this method getting call?Welch
you have to add gesture [view addGestureRecognizer:longPress];Parhe
@EI Captain still not getting as i want.Welch
G
0
UIPinchGestureRecognizer *pinchGesture = ... //initialize
pinchGesture.delegate = self;
//same for rotation gesture
//and then implement delegate method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
Gaze answered 25/4, 2016 at 12:35 Comment(1)
Yes you are right. I solved my problem, only forget to set delegate no need of use the above methods.Welch
W
0

Set the gesture like this.

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

Handle the action

 -(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];
}
}
Welch answered 25/4, 2016 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.