Need to apply UIRotationGestureRecognizer followed by UILongPressGestureRecongnizer
Asked Answered
R

1

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!

Rennold answered 22/4, 2016 at 7:17 Comment(0)
A
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.

Assoil answered 22/4, 2016 at 7:34 Comment(7)
Yes I want, the rotation to occur only after longPress. Thanks for your answer. Let me try it.Rennold
Superb! Works perfect! This what I exactly want. Thanks!Rennold
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?Rennold
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.Assoil
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..Rennold
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. :)Assoil
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!Rennold

© 2022 - 2024 — McMap. All rights reserved.