gpt4 book ai didi

ios - ImageView 没有响应触摸事件

转载 作者:行者123 更新时间:2023-12-01 17:59:53 24 4
gpt4 key购买 nike

我有一个缩略图网格,当其中一个被触摸时,我想显示整个图像。现在,我知道 UIImageView 不响应触摸事件,但正如 this answer 中所建议的那样。 ,我创建了一个 UIButton 来处理事件。请参见下面的代码:

- (void)displayImage
{
NSUInteger i = 0; // The actual code is different and works; this is just for brevity's sake.

// UILazyImageView is a subclass of UIImageView
UILazyImageView *imageView = [[UILazyImageView alloc] initWithURL:[NSURL URLWithString:[[[self images] objectAtIndex:i] thumbnailUrl]]];

UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setFrame:[imageView frame]];
[imageButton addTarget:self action:@selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:imageButton];

[[self containerView] addSubview:imageView];
[imageView release];
}
}

- (void)imageTapped:(id)sender
{
NSLog(@"Image tapped.");

// Get the index of sender in the images array
NSUInteger index = 0; // Don't worry, I know. I'll implement this later

FullImageViewController *fullImageViewController = [[[FullImageViewController alloc] initWithImageURL:[NSURL URLWithString:[[[self images] objectAtIndex:index] url]]] autorelease];
[[self navigationController] pushViewController:fullImageViewController animated:YES];
}

行。所以我创建了一个自定义按钮,设置它的框架以匹配图像框架,告诉它响应内部的修饰以及如何响应,并将其添加到图像的 subview 中。但是当我运行它时,我什么也得不到。 “图像被窃听”。没有出现在控制台中,所以我知道消息没有被发送。我在这里做错了什么?

非常感谢您的帮助。

最佳答案

默认情况下,UIImageView 的 userInteractionEnabled 属性设置为 NO

添加这一行

imageView.userInteractionEnabled = YES;

关于ios - ImageView 没有响应触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11762413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com