user567
user567

Reputation: 3862

Gesture recognizer and UIImageView

I found this code to change the image when it is clicked.

in .h

@interface MyappViewController : UIViewController 
{
    NSDictionary *ddata;
    UIImageView *firstImage;
}
@property(retain,nonatomic) IBOutlet UIImageView *firstImage;

in .m

- (void)viewDidLoad
{
    [super viewDidLoad];

    firstImage.userInteractionEnabled = YES;
    UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc] 
                                     initWithTarget:self action:@selector(clickHandler:)];
    pgr.delegate = self;
    [firstImage addGestureRecognizer:pgr];
    [pgr release];
  //  [self clickHandler:self];  
}

-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    /* destroy the transition view, set the image first */
    UIImageView *transitionImageView = (UIImageView *)context;
    self.firstImage.image = transitionImageView.image;
    [transitionImageView removeFromSuperview];
    transitionImageView = nil;
}

- (void)clickHandler:(id)sender {
    /* temporary view for the animation */
    NSLog(@"Click Handled ");

    UIImageView *transitionImageView = [[UIImageView alloc] initWithFrame:self.firstImage.frame];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[ddata objectForKey:@"pic"]]]];
    transitionImageView.image =  image;
    transitionImageView.alpha = 0.0f;
    [self.view addSubview:transitionImageView];

    [UIView beginAnimations:@"UpdateImages" context:transitionImageView];
    [UIView setAnimationDuration:2.0f];    
    transitionImageView.alpha = 1.0f;
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];    
    [UIView commitAnimations];
}

When I click the image nothing happens, but if I call [self clickHandler:self]; in ViewDidLoad, the image changes. My problem is that the click is not handled when I click the image.

Upvotes: 0

Views: 2183

Answers (1)

Tim
Tim

Reputation: 5054

Instead of a UIPinchGestureRecognizer you need to use a UITapGestureRecognizer. Don't forget to set things like the number of taps required and number of fingers either. The docs are very good for gesture recognizers.

Upvotes: 3

Related Questions