我的应用程序中有一个 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];
最佳答案
确保UIScrollView
属性(property)canCancelContentTouches
设置为 YES
.
scrollView.canCancelContentTouches = YES;
此外,一个 UIScrollView
实现一个方法 touchesShouldCancelInContentView
.
The default returned value is YES if view is not a UIControl object; otherwise, it returns NO.
这意味着UIScrollView
不会尝试取消 UIButtons
中的触摸这可以防止滚动。此外,您可以子类化 UIScrollView
并覆盖 touchesShouldCancelInContentView
返回 YES
当内容 View 对象是 UIButton
时.
关于ios - 如何允许在 UIButtons 上滑动来滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22056139/