ios - 如何允许在 UIButtons 上滑动来滚动?

标签 ios objective-c cocoa-touch uibutton

我的应用程序中有一个 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/

相关文章:

iphone - MPMoviePlayerController - 如何判断正在播放的比特率?

ios - 为什么此 Objective C 代码会因 EXC_BAD_ACCESS code=1 而崩溃? (使用 block 语法和字典)

ios - 记录我的应用程序是否收到通知

iphone - 为什么当我在 -drawRect :? 中绘制字符串时换行符不起作用

cocoa-touch - 核心数据: Can a Large Background Fetch Block Fetch Request on the Main Thread?

ios - Xamarin ios找不到默认值568 @ 2x

android - 如何为 Cordova Ios/Windows Phone/Android 正确设置 "content-security-policy"?

ios - 如何在ipad应用程序的后台线程中显示UIAlertView?

ios - 如何使 native iOS 应用程序响应 UIWebView 中的单击事件?

c++ - OpenAL 中播放缓冲区之间的静音?