取消View上的多个触摸

时间:2013-10-25 14:54:29

标签: ios objective-c uiview uiimageview uigesturerecognizer

我正在添加UIPanGestureRecogniser来拖放视图中的图像。我想一次只拖动一个图像。但我能够一次拖动两个图像。这不应该发生。

我从早上开始就遇到了这个问题。我尝试了在谷歌上找到的所有方法。

- (void)viewDidLoad
{
 [super viewDidLoad];
[self.view setMultipleTouchEnabled:NO];
for(UIImageView *iView in self.movableArray){
    if ([iView isMemberOfClass:[UIImageView class]]){
        UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        [iView addGestureRecognizer:recognizer];
        [iView setUserInteractionEnabled:YES];
        recognizer.delegate = self;
    }
}
}



 - (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
     [gameView bringSubviewToFront:[(UIPanGestureRecognizer *)recognizer view]];
     CGPoint translation = [recognizer translationInView:gameView];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                     recognizer.view.center.y + translation.y);
     [recognizer setTranslation:CGPointMake(0, 0) inView:gameView];
     self.dragObjectImageView =  (UIImageView*)recognizer.view;


if(recognizer.state == UIGestureRecognizerStateBegan){
     int ind = [self.movableArray indexOfObject:self.dragObjectImageView];
    for(int i = 0 ; i < [self.movableArray count] ; i ++){
        if( i != ind ){
            [[self.movableArray objectAtIndex:i] removeGestureRecognizer:recognizer];
        }

    }

    self.homePosition = self.dragObjectImageView.frame;
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
    CGPoint touchPoint = [recognizer locationInView:gameView];
    for (UIImageView *iView in self.staticArray) {
        if ([iView isMemberOfClass:[UIImageView class]]) {
            if (touchPoint.x > iView.frame.origin.x &&
                touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
                touchPoint.y > iView.frame.origin.y &&
                touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
            {
                self.dropTargetImageView = iView;
            }
        }
    }

    if(self.dragObjectImageView.tag == self.dropTargetImageView.tag){
        self.dragObjectImageView.frame = CGRectMake(self.dropTargetImageView.frame.origin.x, self.dropTargetImageView.frame.origin.y + self.dropTargetImageView.frame.size.height/2 - 15, self.dragObjectImageView.frame.size.width, self.dragObjectImageView.frame.size.height);
        [self.dragObjectImageView removeGestureRecognizer:recognizer];
        }
    }else{
        self.dragObjectImageView.frame = self.homePosition;

    }
}
}

2 个答案:

答案 0 :(得分:1)

这是因为您为每个UIPanGestureRecognizer添加了一个imageViews。请尝试仅向self.view添加一个(imageViewssetUserInteractionEnabled:NO,否则他们会抓住这一点。还放

recognizer.maximumNumberOfTouches = 1;

在将其添加到视图之前。您现在要做的就是测试应该在handlePan方法中拖动哪个图像视图。您应该检查识别器状态,当它转向UIGestureRecognizerStateBegan时,您应该保存被拖动的imageView。然后,状态为UIGestureRecognizerStateChanged,只需拖动视图即可。最常见的方法是找出哪个视图被触摸(因为我不知道你的全视图层次结构)将是这样的:

NSUInteger index = [self.movableArray indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop)){
    UIView* hitTest = (UIView*)obj;
    return [hitTest pointInside:firstTouch withEvent:nil];
}];
if ( index != NSNotFound )
   self.draggingView = self.moveableArray[index];
else
   self.draggingView = nil;

当然,如果self.draggingView为nil,那么当用户平移时你就什么都不做。

答案 1 :(得分:0)

您应该在

时为setUserInteractionEnable = NO创建机制
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer; 

在其他图像上调用。您可以执行此操作,也可以停用其他UIPanGestureRecognizer,例如:myPanGestureRecognizer.enabled = NO;

一个简单的例子:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer
{
  UIImageView *currentDraggedImageView = recognizer.view;
  // Based on this, you can iterate again on your UIImageViews and disable them.
  // Once your work is done with gesture recogniser, you can re-enable them.
}