1

Applied UILongPressGestureRecongnizer on one view, Check below code for reference..

@interface ViewController ()
{
     UIRotationGestureRecognizer *rotationGestureRecognizer6;
}


- (void)viewDidLoad {

    //--------Added LongPress Gesture----------//
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                               initWithTarget:self
                                               action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = 2.0;
    [view6 addGestureRecognizer:longPress];

    rotationGestureRecognizer6 = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationWithGestureRecognizer:)];
}

#pragma mark - UILongPressGesture Handler Method

-(void)handleLongPress:(UILongPressGestureRecognizer *)sender {

    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"UIGestureRecognizerStateEnded");
    }
    else if (sender.state == UIGestureRecognizerStateBegan){
        NSLog(@"UIGestureRecognizerStateBegan.");
        [view6 addGestureRecognizer:rotationGestureRecognizer6];
    }
}

#pragma mark - UIRotationGesture Handler Method

-(void)handleRotationWithGestureRecognizer:(UIRotationGestureRecognizer *)recognizer {

    UIView *view = [recognizer view];
    [view setTransform:CGAffineTransformRotate([view transform], [recognizer rotation])];
}

Even I had tried adding Rotation Gesture in other states of UILongPressGestureRecongnizer such as UIGestureRecognizerStateRecognized,UIGestureRecognizerStateChanged,UIGestureRecognizerStatePossible. Not a single one worked for me.

What problem I am facing is, Once logpress gesture detects, it is not adding rotation gesture for the same finger touch. I must need to left that finger touch and again when I tried to rotate it will work well. But I want to allow user to start rotation as soon as longpress gesture detect.

Any help is appreciated! Thanks in advance!

iGatiTech
  • 2,306
  • 1
  • 21
  • 45

1 Answers1

2

You might want the view to respond to multiple gesture recognisers together.

When you can call method of longPressGestureRecognizer and set a Bool,

didReceiveLongPress = YES;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    if(didReceiveLongPress)
       return YES;
    else
       return NO;
}

I assume you want, the rotation to occur only after longPress. Or you can remove the IF case and directly return YES.

user3300864
  • 150
  • 7
  • Yes I want, the rotation to occur only after longPress. Thanks for your answer. Let me try it. – iGatiTech Apr 22 '16 at 07:42
  • 1
    Superb! Works perfect! This what I exactly want. Thanks! – iGatiTech Apr 22 '16 at 07:50
  • Hey! I am wondering. Why your solution working alternatively? 1st time its not working then 2nd time when I apply rotation it is working well then again 3rd time it is not working. What I have done is, assign delegate to longpress gesture in my code and implemented your provided method. This method called alternative. Dont what I am doing wrong. Do you have any idea? – iGatiTech Apr 22 '16 at 09:27
  • What might be happening here is, 1st time; your shouldRecognizeSimultaneouslyWithGestureRecognizer: is called the bool is by default NO, thus it won't recognise rotation. Now, when you longPress your bool is set. So, 2nd time it recognises rotation. Again you might have reset the boolen; so it won't work for 3rd time..but work for 4th..so on. – user3300864 Apr 22 '16 at 10:45
  • No it's not like that. I didn't use boolean anywhere. I simply add methods like this, - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } Still it is working alternatively. Don't know why.. – iGatiTech Apr 22 '16 at 11:00
  • check that your app supports all the rotation, like when your iPhone is UpsideDown as well. It should not happen that the recogniser is not called at all. You will have to do a a bit more digging. :) – user3300864 Apr 22 '16 at 12:26
  • Yes Exactly! I am also plucking my hair! What is going wrong. Alternatively method calling is something what I feel strange. So definitely something is wrong at my end. Need to figure it out! – iGatiTech Apr 22 '16 at 12:30