Need to apply UIRotationGestureRecognizer followed

2019-07-29 07:16发布

问题:

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!

回答1:

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.