Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

ios - Add scrollable button to scrollview

I want to scroll five buttons inside a scrollview. When the user stops dragging a button it should move on to the next button. What am I doing wrong here?

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)
    }
}

The scrollview scrolls well.. but only twice. It Doesn't scroll to next Button. What should I do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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)
    }
}

Also made a demo project in Github
https://github.com/rishi420/ScrollViewCustomPaging


Update:

Trying to consider for the scrolling velocity when user 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, scrollView.contentOffset.y), animated: true)
}

.

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
{
    velocityX = velocity.x
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...