Need to apply UIRotationGestureRecognizer followed by UILongPressGestureRecongnizer

孤人 提交于 2019-12-11 11:12:44

问题


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.



来源:https://stackoverflow.com/questions/36787393/need-to-apply-uirotationgesturerecognizer-followed-by-uilongpressgesturerecongni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!