0

I am using Xcode 4.4 and target is iOS 5.

As on the title, I would like to create multiple pinch gestures for different images. I use two type of images. One is set as a background and the 2nd type of image is a stamp that is pasted on the image that is set as background. To do this I am using UIGestureRecognizer. Everything is fine until I paste the stamp type of image and try to zoom that image. I can zoom but when I zoom the image, the image has has been set as background zooms as well.

With the code below, I can paste different images (stamp type), tap and drag them individually but not when I try all the content zooms as well. Why isn't working?

Thank you in advance

   - (void)viewDidLoad{

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"menuListType=%@ ; menuID=%@", self.category.menuListType ,self.menu.menuID);

UIImage *aImage; 
NSString *imagepath = [NSString stringWithFormat:@"%@%@",[FileUtility getPDFImageFolderPath], self.menu.imageID];
if (self.menu.imageID == nil || [self.menu.imageID isEqualToString:@""]) {
    //        self.canvasView.image = [UIImage imageNamed:@"NotExistFile.jpg"];
    aImage = [UIImage imageNamed:@"NotExistFile.jpg"];

} else {
    //        self.canvasView.image = [UIImage imageWithContentsOfFile:imagepath];
    aImage = [UIImage imageWithContentsOfFile:imagepath];
    //        self.canvasView = [[[UIImageView alloc] initWithImage:self.canvasView.image] autorelease];
}

// Choose image base and set as background
if ([self.category.menuListType isEqualToString:@"4"]) {
    //set size
    int imageW = aImage.size.width;
    int imageH = aImage.size.height;

    float scale = (imageW > imageH ? 500.0f/imageH : 500.0f/imageW);


    CGSize resizedSize = CGSizeMake(imageW * scale, imageH * scale);
    UIGraphicsBeginImageContext(resizedSize);
    [aImage drawInRect:CGRectMake(0, 0, resizedSize.width, resizedSize.height)];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.canvasView.contentMode = UIViewContentModeTop;
    self.canvasView.image = resizedImage;

    if (self.menu.imageID == nil || [self.menu.imageID isEqualToString:@""]) {
    } else {
        self.canvasView = [[[UIImageView alloc] initWithImage:self.canvasView.image] autorelease];
    }


} else {
    // Paste image and set as background
    UIImage *captureImage;
    NSString *capturepath = [NSString stringWithFormat:@"%@%@",[FileUtility getPDFImageFolderPath], @"capture.png"];
    if ([FileUtility fileExist:capturepath]) {
        captureImage = [UIImage imageWithContentsOfFile:capturepath];
    } else {
        captureImage = [UIImage imageNamed:@"NotExistFile3.png"];
    }
    self.canvasView.contentMode = UIViewContentModeTop;;
    self.canvasView.image = captureImage;
    self.canvasView = [[[UIImageView alloc] initWithImage:self.canvasView.image] autorelease];

    // Prepare Gestures
    iViewsTapidx = 0;
    iViewsDblTapidx = 0;
    ivMax = sizeof(iViews) / sizeof(iViews[0]);
    isTaped = NO;
    isDblTaped = NO;
    _width = 100;


    // Initialization 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];

    // drag
    UIPanGestureRecognizer *pan =
    [[UIPanGestureRecognizer alloc] initWithTarget:self 
                                            action:@selector(panAction:)];
    [self.itemView addGestureRecognizer:pan];


    //pinch

    UIPinchGestureRecognizer *pinchGestureRognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
    [self.itemView addGestureRecognizer:pinchGestureRognizer];

    // Stamp Image
    int i = 0;
    CGPoint tapPoint = CGPointMake(self.canvasView.center.x, self.canvasView.center.y);

    int parcent = aImage.size.width / _width;   // Prepare image to paste
    // Show stamp
    *(iViews+i) = [[UIImageView alloc] initWithImage:aImage];
    (*(iViews+i)).frame =  CGRectMake(tapPoint.x - aImage.size.width/parcent/2, 
                                      tapPoint.y - aImage.size.height/parcent/2,
                                      aImage.size.width/parcent,
                                      aImage.size.height/parcent);
    (*(iViews+i)).tag = i+1;
    [self.itemView addSubview:*(iViews+i)];
    iViewsTapidx = i;
    isTaped = YES;
    self.itemView.userInteractionEnabled = YES;

}

[self showAnimation];
}

// 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;
    }
}

// remove
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) {

    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) {
        //Stamp imapge
        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;   // Set stamp image
        //Sow stamp image
        *(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;
    }

}
}

// drag
- (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];
}
}



-(void) handlePinch:(UIGestureRecognizer *) sender {

UIPinchGestureRecognizer *pinchGesture = (UIPinchGestureRecognizer *) sender;

if (pinchGesture.state == UIGestureRecognizerStateBegan || pinchGesture.state == UIGestureRecognizerStateChanged) {

    //UIView *view = pinchGesture.view;
   // UIView *view = self.itemView;
    self.itemView.transform = CGAffineTransformScale(self.itemView.transform, pinchGesture.scale, pinchGesture.scale);
    pinchGesture.scale = 1;
    NSLog(@"zoom");
}
} 
xiltepin
  • 80
  • 1
  • 7

2 Answers2

0

You will have to add Gesture Recognizer on Uiimageview, Look here for doing so : UIGestureRecognizer on UIImageView

Community
  • 1
  • 1
spider1983
  • 1,098
  • 1
  • 7
  • 14
  • I have done that, but all images zoom in/out at the same time. I only want to zoom the one is taped. I forgot to mention that the user brings the image from another view and I am using UILongPressGestureRecognizer and UIPanGestureRecognizer for that – xiltepin Nov 08 '12 at 08:03
  • can you post the code where you have added your gesture to uiimageview – spider1983 Nov 08 '12 at 08:16
  • In here ` CGPoint p = [pinchGestureRecognizer translationInView:self.itemView];` All your image view is subview of "self.itemView" and above line makes all the imageview to respond to your gesture i guess that's the problem. – spider1983 Nov 08 '12 at 09:42
0

Yet another way is to use UIScrolliew as your container instead of UIView and use the - (UIView*)viewForZoomingInScrollView:(UIScrollView *)aScrollView { return imageView; }

here imageView is the image you want to zoom,this will zoom that only image you are pinching its tested and works fine.

spider1983
  • 1,098
  • 1
  • 7
  • 14