ios – 将滚动按钮添加到滚动视图

我想在scrollview中滚动五个按钮.当用户停止拖动按钮时,应该移动到下一个按钮.我在这里做错了什么?
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)
    }
}

滚动浏览很好..但只有两次.它不滚动到下一个按钮.我该怎么办?

解决方法

func setContentOffset(scrollView: UIScrollView) {

    let numOfItems = itemCount  // 5
    let stopOver = scrollView.contentSize.width / CGFloat(numOfItems)
    let x = round(scrollView.contentOffset.x / stopOver) * stopOver

    guard x >= 0 && x <= scrollView.contentSize.width - scrollView.frame.width else {
        return
    }

    scrollView.setContentOffset(CGPointMake(x,scrollView.contentOffset.y),animated: true)
}
extension ViewController: uiscrollviewdelegate {

    func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {

        setContentOffset(scrollView)
    }

    func scrollViewDidEndDragging(scrollView: UIScrollView,willDecelerate decelerate: Bool) {

        guard !decelerate else {
            return
        }

        setContentOffset(scrollView)
    }
}

还在Github https://github.com/rishi420/ScrollViewCustomPaging中做了一个演示项目

更新:

尝试在用户endDragging时考虑滚动速度.

var veLocityX = CGFloat(0.0)

.

func setContentOffset(scrollView: UIScrollView) {

    let numOfItems = itemCount
    let stopOver = scrollView.contentSize.width / CGFloat(numOfItems)
    var x = round((scrollView.contentOffset.x + (veLocityX * 150)) / stopOver) * stopOver // 150 is for test. Change it for your liking

    x = max(0,min(x,scrollView.contentSize.width - scrollView.frame.width))

    scrollView.setContentOffset(CGPointMake(x,animated: true)
}

.

func scrollViewWillEndDragging(scrollView: UIScrollView,withVeLocity veLocity: CGPoint,targetContentOffset: UnsafeMutablePointer<CGPoint>)
{
    veLocityX = veLocity.x
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...