手势识别器和 UIImageView

发布于 2024-12-22 08:53:03 字数 1862 浏览 1 评论 0原文

我发现这段代码可以在单击图像时更改图像。

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 中,图像发生变化。我的问题是,当我单击图像时,单击未得到处理。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2024-12-29 08:53:03

您需要使用 UITapGestureRecognizer 而不是 UIPinchGestureRecognizer。不要忘记设置所需的点击次数和手指数量等内容。这些文档对于手势识别器非常有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文