带有UIButtons的UIScrollview-如何重新创建跳板? [英] UIScrollview with UIButtons - how to recreate springboard?

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

问题描述

我正在尝试在我的应用程序中创建类似于跳板的界面.我正在尝试使用添加到UIScrollView的UIButton.我遇到的问题是按钮没有将任何触摸传递给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-如何重新创建跳板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆