需要先应用 UIRotationGestureRecognizer,再应用 UILongPressGestureRecongnizer

Need to apply UIRotationGestureRecognizer followed by UILongPressGestureRecongnizer

在一个视图上应用了 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])];
}

甚至我曾尝试在 UILongPressGestureRecongnizer 的其他状态下添加旋转手势,例如 UIGestureRecognizerStateRecognizedUIGestureRecognizerStateChangedUIGestureRecognizerStatePossible。没有一个对我有用。

我面临的问题是,一旦检测到 logpress 手势,它不会为同一手指触摸添加旋转手势。我必须离开那个手指触摸,当我试图旋转它时,它会再次工作。但我想让用户在检测到长按手势后立即开始旋转。

感谢任何帮助! 提前致谢!

您可能希望视图同时响应多个手势识别器。

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

didReceiveLongPress = YES;

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

我假设您想要旋转 仅在 长按之后发生。或者你可以去掉 IF 的大小写,直接 return YES.