【发布时间】:2016-05-06 06:18:02
【问题描述】:
我想在滚动视图中滚动五个按钮。当用户停止拖动按钮时,它应该移动到下一个按钮。我在这里做错了什么?
class ViewController: UIViewController {
@IBOutlet weak var categoryScrollView: UIScrollView!
var categoryArr = ["Jack","Mark","Down","Bill","Steve"]
override func viewDidLoad() {
super.viewDidLoad()
let scrollingView = colorButtonsView(CGSizeMake(150,categoryScrollView.frame.size.height), buttonCount: 5)
categoryScrollView.contentSize = scrollingView.frame.size
categoryScrollView.addSubview(scrollingView)
categoryScrollView.showsVerticalScrollIndicator = false
categoryScrollView.delegate = self
categoryScrollView.pagingEnabled = true
categoryScrollView.indicatorStyle = .Default
}
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)
buttonView.addSubview(button)
}
return buttonView
}
}
extension ViewController:UIScrollViewDelegate{
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let index = round(scrollView.contentOffset.x / scrollView.frame.size.width)
print(index)
}
}
滚动视图滚动良好.. 但只有两次。它不会滚动到下一个按钮。我该怎么办?
【问题讨论】:
-
在我的回答中使用代码。它对我来说很好,并且一次在水平方向滚动一个按钮
-
你在使用自动布局吗?
标签: ios swift scrollview