手势识别器和UIImageView

时间:2011-12-19 19:04:36

标签: iphone ios uigesturerecognizer

我发现此代码在单击时更改了图像。

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

当我点击图像时没有任何反应,但如果我打电话给[self clickHandler:self];在ViewDidLoad中,图像会发生变化。我的问题是单击图像时没有处理点击。

1 个答案:

答案 0 :(得分:3)

而不是UIPinchGestureRecognizer,您需要使用UITapGestureRecognizer。不要忘记设置所需的水龙头数量和手指数量。文档非常适合手势识别器。