我正在尝试在我的应用程序中创建类似跳板的界面.我正在尝试将UIButtons添加到UIScrollView中.我正在运行的问题是按钮没有通过任何触摸到UIScrollView - 如果我试图轻弹/滑动并碰巧按下按钮它没有注册UIScrollView,但如果我轻弹之间的空间按钮它会工作.如果我触摸按钮,按钮会单击/工作.
是否存在强制按钮将触摸事件发送到其父级(超级视图)的属性或设置?在添加UIScrollView之前,是否需要将按钮添加到其他内容?
这是我的代码:
//init scrolling area UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 480, 480)]; scrollView.contentSize = CGSizeMake(480, 1000); scrollView.bounces = NO; scrollView.delaysContentTouches = NO; //create background image UIImageView *rowsBackground = [[UIImageView alloc] initWithImage:[self scaleAndRotateImage:[UIImage imageNamed:@"mylongbackground.png"]]]; rowsBackground.userInteractionEnabled = YES; //create button UIButton *btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; btn.frame = CGRectMake(100, 850, 150, 150); btn.bounds = CGRectMake(0, 0, 150.0, 150.0); [btn setImage:[self scaleAndRotateImage:[UIImage imageNamed:@"basicbutton.png"]] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; //add "stuff" to scrolling area [scrollView addSubview:rowsBackground]; [scrollView addSubview:btn]; //add scrolling area to cocos2d //this is just a UIWindow [[[Director sharedDirector] openGLView] addSubview:scrollView]; //mem-mgmt [rowsBackground release]; [btn release]; [scrollView release];
小智.. 44
适用于我的解决方案包括:
设置canCancelContentTouches
在UIScrollView
到YES
.
扩展UIScrollView
为覆盖touchesShouldCancelInContentView:(UIView *)view
以返回YES
时view
为a UIButton
.
根据文档touchesShouldCancelInContentView
返回" YES
取消进一步触摸消息以查看,NO
以使视图继续接收这些消息.默认返回值是YES
视图不是UIControl
对象;否则,它返回NO
."
因为UIButton
是一个UIControl
扩展是必须的,canCancelContentTouches
以实现滚动.
适用于我的解决方案包括:
设置canCancelContentTouches
在UIScrollView
到YES
.
扩展UIScrollView
为覆盖touchesShouldCancelInContentView:(UIView *)view
以返回YES
时view
为a UIButton
.
根据文档touchesShouldCancelInContentView
返回" YES
取消进一步触摸消息以查看,NO
以使视图继续接收这些消息.默认返回值是YES
视图不是UIControl
对象;否则,它返回NO
."
因为UIButton
是一个UIControl
扩展是必须的,canCancelContentTouches
以实现滚动.
为了UIScrollView
确定通过其内容视图的点击与变成滑动或捏合的触摸之间的差异,它需要延迟触摸并查看在该延迟期间您的手指是否已移动.通过设置delaysContentTouches
到NO
你上面的例子中,您要阻止这种情况的发生.因此,滚动视图总是将触摸传递给按钮,而不是在事实证明用户正在执行滑动手势时取消它.尝试设置delaysContentTouches
为YES
.
从结构上讲,将滚动视图中托管的所有视图添加到公共内容视图并仅将该视图用作滚动视图的子视图也是一个好主意.
我有类似的情况,UIScrollView上有许多按钮,我想滚动这些按钮.开始时,我将UIScrollView和UIButton都分类.但是,我注意到我的子类UIScrollView没有收到touchesEnded事件,所以我改为只子类UIButton.
@interface MyPhotoButton : UIButton { } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; @end
@implementation MyPhotoButton - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; [self setEnabled:NO]; [self setSelected:NO]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; [self setEnabled:YES]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesCancelled:touches withEvent:event]; [self setEnabled:YES]; } @end