在阵列中一次将UIPanGestureRecognizer限制为1个对象

时间:2012-03-07 04:03:01

标签: objective-c ios xcode uigesturerecognizer

当另一个物体正在使用它时,如何设置平移? 例如,当我在1个对象上平移时,另一个对象不应该移动。目前我能够平移2个对象。

另一种方法是将摇摄最小触摸设置为2,这是我不想要的。

我的主要代码:

    UIPanGestureRecognizer *imagePanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage:)];
    imagePanGesture.delegate = self;
    [tempImageView addGestureRecognizer:imagePanGesture]; 

    [currentImageArray addObject:tempImageView];



- (void)moveImage:(UIPanGestureRecognizer *)recognizer 
{
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

    if (recognizer.state == UIGestureRecognizerStateEnded) 
    {
        CGPoint velocity = [recognizer velocityInView:self.view];
        CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
        CGFloat slideMult = magnitude / 200;

        float slideFactor = 0.1 * slideMult; // Increase for more of a slide
        CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor), 
                                         recognizer.view.center.y + (velocity.y * slideFactor));

        finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
        finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);

        [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            recognizer.view.center = finalPoint;
            } completion:nil];
    } 
}

我想出了怎么做。在平移之前,请比较视图。

if(recognizer.view == [currentImageArray objectAtIndex:iCurrentImageTag])

2 个答案:

答案 0 :(得分:0)

我想出了怎么做。在平移之前,请比较视图。

if(recognizer.view == [currentImageArray objectAtIndex:iCurrentImageTag])

答案 1 :(得分:0)

正确的方法是在GestureRecognizer委托:

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/index.html

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer