UIScrollView 仅接受第二页上的触摸
我有一个子类 UIScrollView
,它将触摸传递到按钮(UIImageViews
)。 ScrollView
启用了分页功能,并以编程方式分布在 2 到 7 个页面之间。
然而,仅当显示第 2 页时才会识别触摸。滚动效果很好。
有什么想法吗?
这是添加 ScrollView 时的设置:(
lsScroll.pagingEnabled = YES;
[lsScroll setCanCancelContentTouches:NO];
lsScroll.frame = CGRectMake(0, 0, 320, 480);
lsScroll.bounds = CGRectMake(0, 0, 320, 480);
lsScroll.contentSize = CGSizeMake(lsScroll.bounds.size.width, lsScroll.bounds.size.height*maxSection);
[lsScroll setScrollEnabled:YES];
lsScroll.delaysContentTouches = YES;
lsScroll.delegate = self;
lsScroll.bounces = NO;
lsScroll.showsHorizontalScrollIndicator = NO;
lsScroll.showsVerticalScrollIndicator = NO;
lsScroll.clipsToBounds = YES;
其中 maxSection)是一个定义页数的变量,
然后以相同的方式添加部分页面:
section1View = [[UIImageView alloc] initWithFrame:self.view.frame];
section1View.bounds = CGRectMake(0, 0, 320, 480);
section1View.frame = CGRectMake(0, 0, 320, 480);
[section1View setBackgroundColor:[UIColor clearColor]];
[lsScroll addSubview:section1View];
section2View = [[UIView alloc] initWithFrame:self.view.frame];
section2View.bounds = CGRectMake(0, 480, 320, 480);
section2View.frame = CGRectMake(0, 480, 320, 480);
[section2View setBackgroundColor:[UIColor clearColor]];
[lsScroll addSubview:section2View];
section3View = [[UIImageView alloc] initWithFrame:self.view.frame];
section3View.bounds = CGRectMake(0, 960, 320, 480);
section3View.frame = CGRectMake(0, 960, 320, 480);
[section3View setBackgroundColor:[UIColor clearColor]];
[lsScroll addSubview:section3View];
更新:然后将 UIImageViews
添加到section#View's 和这些是我正在检查触摸的对象。 section2View 上的 UIImageViews 注册了触摸,但其他部分上的任何 UIImageViews 都没有注册触摸。
一切都显示正确,并且在正确的位置滚动可以工作,如果您在第 2 页/第 2 部分工作,则触摸事件可以工作 - 但所有其他触摸都会失败。
任何帮助不胜感激。
更新:这是添加“UIImageViews”的代码。所以你可以看到我已经把这些都正确了。第一个是第 1 页上出现的“UIImageViw” - 未收到触摸,第二个是第 2 页上的“UIImageView” - 触摸有效。
ls1= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ls1.png"]];
ls1.center = (CGPoint){ 215, 65 };
ls1.transform = CGAffineTransformMakeScale(0.33,0.33);
[ls1 setUserInteractionEnabled: YES];
[section1View addSubview: ls1];
ls20= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ls20.png"]];
ls20.center = (CGPoint){ 215, 615 };
ls20.transform = CGAffineTransformMakeScale(0.33,0.33);
[ls20 setUserInteractionEnabled: YES];
[section2View addSubview: ls20];
这些代码行是使用电子表格中的宏创建的 - 所以我知道它们都是相同的并且都包含 setUserInteractionEnabled。
I have a subclassed UIScrollView
that passes touches onto the buttons(UIImageViews
).
The ScrollView
has paging enabled and is spread across between 2 and 7 pages programmitically.
However the touches are only recognised when page 2 is shown. The scrolling works fine.
Any Ideas?
Here is the settings I have when I add the ScrollView:
lsScroll.pagingEnabled = YES;
[lsScroll setCanCancelContentTouches:NO];
lsScroll.frame = CGRectMake(0, 0, 320, 480);
lsScroll.bounds = CGRectMake(0, 0, 320, 480);
lsScroll.contentSize = CGSizeMake(lsScroll.bounds.size.width, lsScroll.bounds.size.height*maxSection);
[lsScroll setScrollEnabled:YES];
lsScroll.delaysContentTouches = YES;
lsScroll.delegate = self;
lsScroll.bounces = NO;
lsScroll.showsHorizontalScrollIndicator = NO;
lsScroll.showsVerticalScrollIndicator = NO;
lsScroll.clipsToBounds = YES;
(where maxSection) is a variable that defines the number of pages
Section pages are then added in the same manner:
section1View = [[UIImageView alloc] initWithFrame:self.view.frame];
section1View.bounds = CGRectMake(0, 0, 320, 480);
section1View.frame = CGRectMake(0, 0, 320, 480);
[section1View setBackgroundColor:[UIColor clearColor]];
[lsScroll addSubview:section1View];
section2View = [[UIView alloc] initWithFrame:self.view.frame];
section2View.bounds = CGRectMake(0, 480, 320, 480);
section2View.frame = CGRectMake(0, 480, 320, 480);
[section2View setBackgroundColor:[UIColor clearColor]];
[lsScroll addSubview:section2View];
section3View = [[UIImageView alloc] initWithFrame:self.view.frame];
section3View.bounds = CGRectMake(0, 960, 320, 480);
section3View.frame = CGRectMake(0, 960, 320, 480);
[section3View setBackgroundColor:[UIColor clearColor]];
[lsScroll addSubview:section3View];
UPDATE: UIImageViews
are then added to the section#View's and these are the objects that I am checking for touches on. The UIImageViews
that are on section2View register the touch but any UIImageViews
on the other section arent registering touches.
Everything displays correctly and in the correct place the scrolling works, the touch events work if you are on page/section 2 work - but all other touches fail.
Any help gratefully appreciated.
UPDATE: Here is the code for adding the 'UIImageViews'. So you can see that I already have everything correct with these. The first is for a 'UIImageViw that appears on page 1 - touch not recieved and the second for a 'UIImageView' on page 2 - touches do work.
ls1= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ls1.png"]];
ls1.center = (CGPoint){ 215, 65 };
ls1.transform = CGAffineTransformMakeScale(0.33,0.33);
[ls1 setUserInteractionEnabled: YES];
[section1View addSubview: ls1];
ls20= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ls20.png"]];
ls20.center = (CGPoint){ 215, 615 };
ls20.transform = CGAffineTransformMakeScale(0.33,0.33);
[ls20 setUserInteractionEnabled: YES];
[section2View addSubview: ls20];
These lines of code were created using a macro in a spreadsheet - so I know that they are all the same and all contain setUserInteractionEnabled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
section2View 使用
UIView
创建,其余部分使用UIImageView
创建。如果这些图像视图上的userInteractionEnabled
为 NO,则它们的子视图将不会接收触摸。section2View is created with
UIView
, the rest withUIImageView
. IfuserInteractionEnabled
is NO on these image views, their subviews won't receive touches.