2014-02-26 46 views
1

我在我的应用程序中有一个UIButtons的旋转木马菜单。除了按钮有点大以外,一切都很好,当手指拖过按钮时它不会滚动。拖动必须在旋转木马当前和中心的按钮之外完成。是否有一些设置可以更改,以便我可以在按钮上滑动以进行滚动并让它们也可以互动?如何允许通过UIButtons滑动滚动?

下面是一个创建按钮的代码:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(0, 0, 500.0f, 320.0f); 
button.layer.cornerRadius = 8.0f; 
button.layer.masksToBounds = NO; 
button.layer.borderWidth = 0.0f; 

button.layer.shadowColor = [UIColor blackColor].CGColor; 
button.layer.shadowOpacity = 0.8; 
button.layer.shadowRadius = 12; 
button.layer.shadowOffset = CGSizeMake(12.0f, 12.0f); 

[button setImage:(UIImage*)[buttons objectAtIndex:index] forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
+0

当手指拖动按钮时,按钮是第一响应者,所以按钮动作被触发。如果你想跳过按钮动作,你可能会更好地实现你自己的touchMoved,touchBegan,touchEnd代码 – Jing

回答

2

确保UIScrollView属性canCancelContentTouches设置为YES

scrollView.canCancelContentTouches = YES; 

此外,UIScrollView实现方法touchesShouldCancelInContentView

如果view不是UIControl对象,则默认返回值为YES; 否则,它返回NO。

这意味着UIScrollView不会尝试取消防止滚动的触摸UIButtons。另外,当内容视图对象为UIButton时,您可以子类UIScrollView并覆盖touchesShouldCancelInContentView以返回YES

+0

我实际上并没有使用UIScrollView。对困惑感到抱歉。我使用在这里找到的iCarousel类:https://github.com/nicklockwood/iCarousel来创建轮播效果。 – lunadiviner