Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

xiltepin

macrumors newbie
Original poster
Dec 31, 2009
2
0
In my project I have a UIImageVIew where the user imports a backgroudn and then puts smaller pictures on that background.

I want to apply the gestures only to the smaller pictures that are being tapped. The gestures I am using are tap, long press, pand and pinch gestures. My problem is only with pinch gestures. I can apply long press, tap and pan individually BUT with pinch, not only the smaller pictures zoom but also the background. Any idea of how can I fix this?


in my - (void)viewDidLoad I have set:

Code:
        //single tap 
        UITapGestureRecognizer *tapg = 
        [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                action:@selector(tapAction:)];
        [self.itemView addGestureRecognizer:tapg];
        
        // double tap
        UITapGestureRecognizer *doubleTap =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(doubleTapAction:)];
        doubleTap.numberOfTapsRequired = 2;
        [self.itemView addGestureRecognizer:doubleTap];
        
        // longPress
        UILongPressGestureRecognizer *longPress =
        [[UILongPressGestureRecognizer alloc] initWithTarget:self 
                                                      action:@selector(longPressAction:)];
        [self.itemView addGestureRecognizer:longPress];
        
        // pan
        UIPanGestureRecognizer *pan =
        [[UIPanGestureRecognizer alloc] initWithTarget:self 
                                                action:@selector(panAction:)];
        [self.itemView addGestureRecognizer:pan];
        
        
        //pinch
        
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
        [self.itemView addGestureRecognizer:pinch];


then,

Code:
// tap
- (void)tapAction:(UITapGestureRecognizer *) sender{
    CGPoint tapPoint = [sender locationInView:self.itemView];
    NSLog(@">>>tap x=%.2f, y=%.2f", tapPoint.x, tapPoint.y);
    int i =0;
    isTaped = NO;
    for (i = 0; i < ivMax; i++) {
        if (CGRectContainsPoint((*(iViews+i)).frame, tapPoint)) {
            isTaped = YES;
            iViewsTapidx = i;
            NSLog(@"i = %d", i);
            break;
        }
    }
}

// doubleTap
- (void)doubleTapAction:(UITapGestureRecognizer *) sender{
    NSLog(@">>>doubleTap");
    CGPoint tapPoint = [sender locationInView:self.itemView];
    isTaped = NO;
    isDblTaped = NO;
    
    int i =0;
    for (i = 0; i < ivMax; i++) {
        if (CGRectContainsPoint((*(iViews+i)).frame, tapPoint)) {
            isDblTaped = YES;
            iViewsDblTapidx = i;
            break;
        }
    }
    
    // view
    if (isDblTaped) {
        NSLog(@"remove %d", i);
        (*(iViews+i)).tag = 0;
        [*(iViews+i) removeFromSuperview];
    }
}

// longPress
- (void)longPressAction:(UILongPressGestureRecognizer *) sender{
    if ([sender state] == UIGestureRecognizerStateBegan) {

        NSLog(@">>>longPress 1");
    }else if ([sender state] == UIGestureRecognizerStateEnded) {   
        //        NSLog(@">>>longPress 2");
        
        CGPoint tapPoint = [sender locationInView:self.itemView];
        NSLog(@">>>longPress 2 x=%.2f, y=%.2f", tapPoint.x, tapPoint.y);
        int i =0;
        for (i = 0; i < ivMax; i++) {
            NSLog(@"i = %d", i);
            if ((*(iViews+i)).tag == 0) {
                break;
            }
        }
        
        if (i < ivMax) {
            //set smaller picture
            UIImage *stampImage; 
            NSString *imagepath = [NSString stringWithFormat:@"%@%@",[FileUtility getPDFImageFolderPath], self.menu.imageID];
            if (self.menu.imageID == nil || [self.menu.imageID isEqualToString:@""]) {
                stampImage = [UIImage imageNamed:@"NotExistFile.jpg"];
                
            } else {
                stampImage = [UIImage imageWithContentsOfFile:imagepath];
            }
            int parcent = stampImage.size.width / _width;   

            //show smaller picture
            *(iViews+i) = [[UIImageView alloc] initWithImage:stampImage];
            (*(iViews+i)).frame =  CGRectMake(tapPoint.x - stampImage.size.width/parcent/2, 
                                              tapPoint.y - stampImage.size.height/parcent/2, 
                                              stampImage.size.width/parcent,
                                              stampImage.size.height/parcent);
            (*(iViews+i)).tag = i+1;
            [self.itemView addSubview:*(iViews+i)];
            iViewsTapidx = i;
            isTaped = YES;
        }
        
    }
}

// Pan
- (void)panAction:(UIPanGestureRecognizer *) sender{
    NSLog(@">>>pan");
    if (isTaped) {
       
        CGPoint p = [sender translationInView:self.itemView];
        CGPoint movePoint = CGPointMake((*(iViews+iViewsTapidx)).center.x + p.x, 
                                        (*(iViews+iViewsTapidx)).center.y + p.y);
        (*(iViews+iViewsTapidx)).center = movePoint;
        //        NSLog(@">>>pan x=%.2f, y=%.2f --> x=%.2f, y=%.2f", p.x, p.y, movePoint.x, movePoint.y);
        NSLog(@">>>pan x=%.2f, y=%.2f", p.x, p.y);
        [sender setTranslation:CGPointZero inView:self.itemView];
    }
}

//iViews.userInteractionEnabled = YES;

// scale the piece by the current scale
// reset the gesture recognizer's rotation to 0 after applying so the next callback is a delta from the current scale
- (void)pinchAction:(UIPinchGestureRecognizer *)sender
{  
    if ([sender state] == UIGestureRecognizerStateBegan || [sender state] == UIGestureRecognizerStateChanged) {
        [sender view].transform = CGAffineTransformScale([[sender view] transform], [sender scale], [sender scale]);
        [sender setScale:1];
    
 }
                                                      
    }
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.