Xcode how to set tapGuesture on UIImage

in your viewDidLoad method
CGRect frame = CGRectMake(0, 50, 320, 200);
    
    //create the object for the imageview
    imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.image = [UIImage imageNamed:@"p5.png"];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    imageView.userInteractionEnabled = YES;
    imageView.multipleTouchEnabled = YES;
    
    [self.view addSubview:imageView];
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    //tap gesture
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] 
                                          initWithTarget:self 
                                          action:@selector(handleTapGesture:)];
    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

implement handleTapGesture method when you tap on image.
-(IBAction)handleTapGesture:(UIGestureRecognizer *)sender {
    if (sender.view.contentMode == UIViewContentModeScaleAspectFit)
        sender.view.contentMode = UIViewContentModeCenter;
    else
        sender.view.contentMode = UIViewContentModeScaleAspectFit;
}

this is your p5.png image

You can download the Example here

0 comments:

Post a Comment