需要申请UIRotationGestureRecognizer随后UILongPressGestur

2019-09-26 16:33发布

施加UILongPressGestureRecongnizer上的一个视图,检查下面的代码以供参考..

@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])];
}

即使我曾试图在其他国家加入旋转手势UILongPressGestureRecongnizerUIGestureRecognizerStateRecognizedUIGestureRecognizerStateChangedUIGestureRecognizerStatePossible 。 没有一个为我工作。

我面临什么问题是,一旦logpress手势检测,它不加入旋转手势为同一手指触摸。 我必须要当我试图将其旋转将工作做好,并再次留下了手指触摸。 但我想,让用户尽快长按手势检测开始旋转。

任何帮助表示赞赏! 提前致谢!

Answer 1:

您可能希望视图一起到多个手势识别器响应。

当你可以调用longPressGestureRecognizer的方法,并设置一个布尔,

didReceiveLongPress = YES;

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

我假设你想要的, 只是长按发生旋转。 或者你可以删除if情况下,直接返回YES。



文章来源: Need to apply UIRotationGestureRecognizer followed by UILongPressGestureRecongnizer