UIScrollView 内视图中的 UIgestureRecognizer
有没有人设法让 UIGestureRecognizer 在作为 UIScrollView 子视图的 UIView 上工作?我的回调似乎从未被调用。
作为一个简单的示例,我想要一个分页滚动视图,并在第三页上使用 UITapGestureRecognizer 监听点击。但是我无法让它工作。
我会这样做:
self.scrollView = [[[UIScrollView alloc] initWithFrame:self.view.frame] autorelease];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(self.section1ScrollView.frame.size.width * 3, self.scrollView.frame.size.height); //3 pages
UIImageView *p0 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page0.png"]] autorelease];
[self.scrollView insertSubview:p0 atIndex:self.scrollView.subviews.count];
UIImageView *p1 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page1.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p1 atIndex:self.scrollView.subviews.count];
UIImageView *p2 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page2.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p2 atIndex:self.scrollView.subviews.count];
UITapGestureRecognizer *p2TapRegocnizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(p2Tapped:)] autorelease];
//p2TapRegocnizer.delegate = self;
[p2 addGestureRecognizer:p2TapRegocnizer];
Has anyone managed to get a UIGestureRecognizer to work on a UIView that is a subview of a UIScrollView? My callbacks never seems to get called.
As a simple example, I want to have a paging scrollview and on the third page listen for a tap with a UITapGestureRecognizer. However I can not get it to work.
Here's how I would do it:
self.scrollView = [[[UIScrollView alloc] initWithFrame:self.view.frame] autorelease];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(self.section1ScrollView.frame.size.width * 3, self.scrollView.frame.size.height); //3 pages
UIImageView *p0 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page0.png"]] autorelease];
[self.scrollView insertSubview:p0 atIndex:self.scrollView.subviews.count];
UIImageView *p1 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page1.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p1 atIndex:self.scrollView.subviews.count];
UIImageView *p2 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page2.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p2 atIndex:self.scrollView.subviews.count];
UITapGestureRecognizer *p2TapRegocnizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(p2Tapped:)] autorelease];
//p2TapRegocnizer.delegate = self;
[p2 addGestureRecognizer:p2TapRegocnizer];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIImageView 默认 userInteractionEnabled 设置为 NO。我会尝试将其更改为“是”。
UIImageView has in default userInteractionEnabled set to NO. I would try to change it to YES.