UIgestureRecognizer in a view inside a UIScrollView

巧了我就是萌 提交于 2020-01-13 09:04:09

问题


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

回答1:


UIImageView has in default userInteractionEnabled set to NO. I would try to change it to YES.




回答2:


[webView setUserInteractionEnabled:YES]


来源:https://stackoverflow.com/questions/3005903/uigesturerecognizer-in-a-view-inside-a-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!