简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM