0

我正在添加 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;

    }
}
}
4

2 回答 2

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,当用户四处平移时,您将什么也不做。

于 2013-10-25T15:06:27.200 回答
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.
}
于 2013-10-25T15:09:22.830 回答