【发布时间】:2016-05-23 05:33:34
【问题描述】:
UIButtons 内的scrollview 没有触发事件。我尝试设置exclusiveTouch delaysTouchesEvent,但没有任何帮助。
class TestViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
var categoryArr = ["Jack","Mark","Down","Bill","Steve"]
var buttonColors = [UIColor.greenColor(), UIColor.blueColor(), UIColor.blackColor(), UIColor.cyanColor(), UIColor.magentaColor()]
override func viewDidLoad() {
super.viewDidLoad()
let scrollingView = colorButtonsView(CGSizeMake(150,scrollView.frame.size.height), buttonCount: 5)
scrollView.contentSize = scrollingView.frame.size
scrollView.addSubview(scrollingView)
scrollView.showsVerticalScrollIndicator = false
scrollView.delegate = self
scrollView.pagingEnabled = true
scrollView.indicatorStyle = .Default
scrollView.canCancelContentTouches = false
scrollView.delaysContentTouches = false
scrollView.exclusiveTouch = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func colorButtonsView(buttonSize:CGSize, buttonCount:Int) -> UIView {
let buttonView = UIView()
buttonView.frame.origin = CGPointMake(0,0)
let padding = CGSizeMake(10, 10)
buttonView.frame.size.width = (buttonSize.width + padding.width) * CGFloat(buttonCount)
var buttonPosition = CGPointMake(padding.width * 0.5, padding.height)
let buttonIncrement = buttonSize.width + padding.width
for i in 0...(buttonCount - 1) {
var button = UIButton.buttonWithType(.Custom) as! UIButton
button.frame.size = buttonSize
button.frame.origin = buttonPosition
buttonPosition.x = buttonPosition.x + buttonIncrement
button.setTitle(categoryArr[i], forState: UIControlState.Normal)
button.addTarget(self, action: "showTheNextButton:", forControlEvents: UIControlEvents.TouchUpInside)
button.backgroundColor = buttonColors[i]
buttonView.addSubview(button)
}
return buttonView
}
func showTheNextButton(sender:UIButton){
print("ok show the next button")
}
}
extension TestViewController:UIScrollViewDelegate{
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let index = round(scrollView.contentOffset.x / scrollView.frame.size.width)
print(index)
}
}
【问题讨论】:
-
控制台中没有错误信息吗?我最近在控件无响应时遇到了类似的问题,原因是约束配置不正确。
-
没有什么...我在故事板中有滚动视图..我将发布它的屏幕截图
-
您已经通过 IBOutlet 连接在 Storyboard 中使用了 UIScrollview,那么这条线的目的是什么? scrollView.addSubview(scrollingView)
-
我还记得当我想创建可滚动视图时,我最终得到的解决方案是在滚动视图中使用另一个容器 UIView 以及该容器视图中的所有控件。
-
@DmitryKlochkov 我也在做同样的事情...scrollingView 是容器视图并添加到滚动视图...
标签: ios swift uiscrollview uibutton