带有 UIButtons 的 UIScrollview - 如何重新创建跳板?

时间:2022-10-16
本文介绍了带有 UIButtons 的 UIScrollview - 如何重新创建跳板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中创建一个类似跳板的界面.我正在尝试使用添加到 UIScrollView 的 UIButtons.我遇到的问题是按钮没有将任何触摸传递给 UIScrollView - 如果我尝试轻弹/滑动并碰巧按下按钮,它不会注册 UIScrollView,但是如果我轻弹之间的空间按钮它将起作用.如果我触摸它们,按钮会单击/工作.

I'm trying to create a springboard-like interface within my app. I'm trying to use UIButtons added to a UIScrollView. The problem I'm running in to is with the buttons not passing any touches to the UIScrollView - if I try to flick/slide and happen to press on the button it doesn't register for the UIScrollView, but if I flick the space between buttons it will work. The buttons do click/work if I touch them.

是否有强制按钮将触摸事件发送到其父级(超级视图)的属性或设置?在添加 UIScrollView 之前是否需要将按钮添加到其他内容?

Is there a property or setting that forces the button to send the touch events up to its parent (superview)? Do the buttons need to be added to something else before being added the 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];

推荐答案

为了让 UIScrollView 确定点击进入其内容视图和触摸转动之间的区别滑动或捏合时,它需要延迟触摸并查看您的手指是否在此延迟期间移动.通过在上面的示例中将 delaysContentTouches 设置为 NO,您可以防止这种情况发生.因此,滚动视图总是将触摸传递给按钮,而不是在用户执行滑动手势时取消它.尝试将 delaysContentTouches 设置为 YES.

In order for UIScrollView to determine the difference between a click that passes through to its content view(s) and a touch that turns into a swipe or pinch, it needs to delay the touch and see if your finger has moved during that delay. By setting delaysContentTouches to NO in your above example, you're preventing this from happening. Therefore, the scroll view is always passing the touch to the button, instead of canceling it when it turns out that the user is performing a swipe gesture. Try setting delaysContentTouches to YES.

从结构上讲,将要在滚动视图中托管的所有视图添加到公共内容视图并仅使用该视图作为滚动视图的子视图,这也是一个好主意.

It might also be a good idea, structurally, to add all the views to be hosted in your scroll view to a common content view and only use that one view as the scroll view's subview.

这篇关于带有 UIButtons 的 UIScrollview - 如何重新创建跳板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:关闭 UIScrollView 中的键盘 下一条:如何以编程方式创建 UIScrollView?

相关文章

最新文章