1

想知道以下是否可以处理 UIGestureRecognizer 触摸:

        if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged)
        {
            //process touches
        }
        else if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded)
        {
            //do whatever after gesture recognizer finishes
        }

基本上,我想知道是否UIGestureRecognizerStateEnded应该发生UIGestureRecognizer静止过程触摸,或者此时所有触摸都完成了?我得到了奇怪的值,translationInView所以只想在这里问。

4

1 回答 1

1

您问:

基本上,我想知道是否UIGestureRecognizerStateEnded应该发生UIGestureRecognizer静止过程触摸,或者此时所有触摸都完成了?

当你得到UIGestureRecognizerStateEnded时,是的,手势就完成了。但显然,除非您在该点从视图中移除手势识别器,否则如果用户开始一个新手势,则手势识别过程会从UIGestureRecognizerStateBegan.

此外,你说:

我得到了奇怪的值,translationInView所以只想在这里问。

您的代码示例表明您正在处理捏合手势,但这并不适用translationInView,所以我不确定您得到了什么“奇怪的值”。您可以通过设置手势delegate并实现两个同时手势shouldRecognizeSimultaneouslyWithGestureRecognizer

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self
                                                                                action:@selector(handlePinch:)];
    pinch.delegate = self;
    [self.view addGestureRecognizer:pinch];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(handlePan:)];
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]])
        return YES;
    if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
        return YES;
    return NO;
}

- (void)handlePinch:(UIPinchGestureRecognizer *)gesture
{
    CGFloat scale = [gesture scale];
    NSLog(@"%s: %@: scale=%.2f", __FUNCTION__, [self stringFromGestureState:gesture.state], scale);
}

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    CGPoint translation = [gesture translationInView:gesture.view];
    NSLog(@"%s: %@: translation=%@", __FUNCTION__, [self stringFromGestureState:gesture.state], NSStringFromCGPoint(translation));
}

上面的代码有效,其中handlePan返回 pan,handlePinch返回 pinch,并且translationInViewofhandlePan看起来没有异常。也许你可以向我们展示你是如何使用捏合手势和获取translationInView的,并告诉我们你得到的值有什么奇怪的地方。

于 2013-03-26T08:28:11.997 回答