Question

In my view, there's an array of UIImageView. I tried to add panning gesture, my UIImageView is able to move. But the problem is after i insert the second UIImageView into the array, i'm able to move the UIImageView when i touches the previous UIImageView.

How do I limit the touch within the current UIImageView?

- (void)addNewImageToArray: (UIImage *)inImage
{
    UIImageView *tempImageView = [[UIImageView alloc]initWithImage:inImage];
    tempImageView.userInteractionEnabled = YES;
    tempImageView.center = self.vwDesktop.center;

    CALayer * l = [tempImageView layer];
    [l setBorderWidth:2.0];
    [l setBorderColor:[[UIColor grayColor] CGColor]];

    tempImageView.frame = CGRectMake(0,0,inImage.size.width,inImage.size.height);    

    [currentImageArray addObject:tempImageView];

    UIPanGestureRecognizer *stampPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveStamp:)];
    [stampPanGesture setMinimumNumberOfTouches:1];
    [stampPanGesture setMaximumNumberOfTouches:1];
    [[currentImageArray objectAtIndex:currentImageArray.count - 1] addGestureRecognizer:stampPanGesture]; 

    [self.vwDesktop addSubview:[currentImageArray objectAtIndex:currentImageArray.count - 1]];  
}
Was it helpful?

Solution

Is there a reason you don't add the gesture recognizer to the tempImageView before adding it to the currentImageArray?

We really need to see the code in moveStamp. Based on how you are doing this, you'll need to interrogate the recognizer, ask it who its view is, and then move that view. Are you doing that? I don't have code handy but it looks something like:

- (void)moveStamp:(UIPanGestureRecognizer*)panRecognizer {
    UIView *viewToMove = panRecognizer.view;
    // move the view around
}

The panRecognizer will only point to it's view so it should work. I have done this with many objects on screen (see my app QPlus for example), combining pan, pinch and tap gestures and they all play nice together.

Good luck,

Damien

OTHER TIPS

Maybe you can use UIImageView's tag to solve your problem.

- (void)moveStamp:(UIGestureRecognizer *)gesture
{
    NSInteger tag = gesture.view.tag;
    if (tag = currentViewTag) {
        return;
    }
    //...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top