UIScrollView 子视图中的 UITapGestureRecognizer

发布于 2024-10-24 02:16:36 字数 845 浏览 3 评论 0原文

我正在尝试在 imageView (也是滚动视图子项)中使用单击识别器。 在界面生成器中,我仅创建并引用了滚动视图。 可以进行滚动,但无法识别单击事件(不记录任何内容)。 这是代码:

- (void)loadView {
[super loadView];

UIImage *myImage = [UIImage imageNamed:@"img1.jpg"];
imageView = [[UIImageView alloc] initWithImage:myImage];

[myImage release];

// add gesture recognizers to the image view
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

[imageView addGestureRecognizer:singleTap];

[singleTap release];

[imageScrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];

[imageScrollView addSubview:imageView]; }

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
// single tap handling
NSLog(@"sinlgeTap called"); }

我已经环顾了几个小时并尝试了很多东西。也许别人的目光能提供更多帮助。 谢谢。

I'm trying to use a single tap recognizer in an imageView (that is also a scrollView child).
In Interface Builder I've created and referentiated the scrollView only.
Works the scrolling but the single tap event is not recognized (nothing is logged).
Here's the code:

- (void)loadView {
[super loadView];

UIImage *myImage = [UIImage imageNamed:@"img1.jpg"];
imageView = [[UIImageView alloc] initWithImage:myImage];

[myImage release];

// add gesture recognizers to the image view
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

[imageView addGestureRecognizer:singleTap];

[singleTap release];

[imageScrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];

[imageScrollView addSubview:imageView]; }

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
// single tap handling
NSLog(@"sinlgeTap called"); }

I've looked around for hours and tried many things. Maybe a look of someone else can help much more.
Thanks.

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

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

发布评论

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

评论(1

画▽骨i 2024-10-31 02:16:36

我已经为 imageView 启用了 userInteraction ,也为scrollView启用了 canCancelContentTouches 选项:

[imageScrollView setCanCancelContentTouches:YES];
[imageView setUserInteractionEnabled:YES];

它现在可以工作了。

I've enabled the userInteraction for imageView and also the canCancelContentTouches option for the scrollView:

[imageScrollView setCanCancelContentTouches:YES];
[imageView setUserInteractionEnabled:YES];

It works now.

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