简体   繁体   中英

Cancel multiple touches on the View

I am adding UIPanGestureRecogniser to drag and drop images in a view.I want to drag only one image at a time.but I am able to drag two images at a time.which should not happen.

I am stuck with this bug since morning.I tried all the ways i found on google.

- (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;

    }
}
}

This happens because you are adding one UIPanGestureRecognizer for each of your imageViews . Try adding only one to your self.view (and have your imageViews to setUserInteractionEnabled:NO , otherwise they will trap the touch). Also put

recognizer.maximumNumberOfTouches = 1;

Before you add it to your view. All you have to do now is to test which image view should be dragged in your handlePan method. You should check the recognizer state and when it turns to UIGestureRecognizerStateBegan you should save which imageView is being dragged. Then as the state is UIGestureRecognizerStateChanged just drag the view around. The most general way you can find out which view was touched (as i don't know your full view hierarchy) would be to do something like:

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;

Then of course if self.draggingView is nil you would do nothing when the user is panning around.

You should create a mechanism to setUserInteractionEnable = NO when your

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer; 

Is called on the other images. You can either do that, or disable the others UIPanGestureRecognizer , like: myPanGestureRecognizer.enabled = NO;

A quick example:

- (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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM