这是使用UIGestureRecognizer之一处理手势的安全方法吗?

时间:2013-03-26 00:42:29

标签: iphone ios cocoa-touch uigesturerecognizer

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

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

基本上,我想知道UIGestureRecognizerStateEnded是否会在UIGestureRecognizer仍处理过程中发生{或1}},或者在此时是否已完成所有触摸?我为translationInView获得了奇怪的价值,所以我想在这里问一下。

1 个答案:

答案 0 :(得分:1)

你问:

  

基本上,我想知道UIGestureRecognizerStateEnded是否会在UIGestureRecognizer仍处理过程中发生{或1}},或者在此时是否已完成所有触摸?

当你得到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返回平移,而handlePinch返回捏,translationInView的{​​{1}}看起来无异常。也许你可以向我们展示你是如何使用捏合手势并获得handlePan并告诉我们你所获得的价值中有多奇怪。