ios – UIScrollView的平滑自定义分页

前端之家收集整理的这篇文章主要介绍了ios – UIScrollView的平滑自定义分页前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在UIScrollView中有两个(可能更多)视图,并希望使用分页.当我尝试使用UIScrollView的默认分页选项时出现问题,因为视图具有不同的宽度,无法正确分页.

所以我已经实现了一个有效的自定义分页代码.但是,当滚动较慢时,它不会按预期运行. (它回到没有动画的原始位置.)

以下是我目前通过UIScrollViewDelegate进行自定义分页方法

func scrollViewWillEndDragging(_ scrollView: UIScrollView,withVelocity velocity: CGPoint,targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if direction == 1{
        targetContentOffset.pointee.x = 0
    }else{
        targetContentOffset.pointee.x = 100
    }
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: scrollView.superview).x > 0 {
        direction = 1
    }
    else {
        direction = 0
    }
}

我想要的是:

是)我有的:

解决方法

尝试下面的自定义UIScrollView类示例
import UIKit

public class BaseScrollViewController: UIViewController,UIScrollViewDelegate {

    public var leftVc: UIViewController!
    public var middleVc: UIViewController!
    public var rightVc: UIViewController!

    public var initialContentOffset = CGPoint() // scrollView initial offset
    public var maximumWidthFirstView : CGFloat = 0

    public var scrollView: UIScrollView!

    public class func containerViewWith(_ leftVC: UIViewController,middleVC: UIViewController,rightVC: UIViewController) -> BaseScrollViewViewController {
        let container = BaseScrollViewViewController()

        container.leftVc = leftVC
        container.middleVc = middleVC
        container.rightVc = rightVC
        return container
    }

    override public func viewDidLoad() {
        super.viewDidLoad()
        setupHorizontalScrollView()
    }

    func setupHorizontalScrollView() {
        scrollView = UIScrollView()
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false

        let view = (
            x: self.view.bounds.origin.x,y: self.view.bounds.origin.y,width: self.view.bounds.width,height: self.view.bounds.height
        )

        scrollView.frame = CGRect(x: view.x,y: view.y,width: view.width,height: view.height
        )

        self.view.addSubview(scrollView)

        let scrollWidth  = 3 * view.width
        let scrollHeight  = view.height
        scrollView.contentSize = CGSize(width: scrollWidth,height: scrollHeight)

        leftVc.view.frame = CGRect(x: 0,y: 0,height: view.height
        )

        middleVc.view.frame = CGRect(x: view.width,height: view.height
        )

        rightVc.view.frame = CGRect(x: 2 * view.width,height: view.height
        )

        addChildViewController(leftVc)
        addChildViewController(middleVc)
        addChildViewController(rightVc)

        scrollView.addSubview(leftVc.view)
        scrollView.addSubview(middleVc.view)
        scrollView.addSubview(rightVc.view)

        leftVc.didMove(toParentViewController: self)
        middleVc.didMove(toParentViewController: self)
        rightVc.didMove(toParentViewController: self)

        scrollView.contentOffset.x = middleVc.view.frame.origin.x
        scrollView.delegate = self
    }


    public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.initialContentOffset = scrollView.contentOffset
    }

    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if maximumWidthFirstView != 0
        {
            if scrollView.contentOffset.x < maximumWidthFirstView
            {
                scrollView.isScrollEnabled = false
                let newOffset = CGPoint(x: maximumWidthFirstView,y: self.initialContentOffset.y)

                self.scrollView!.setContentOffset(newOffset,animated:  false)
                scrollView.isScrollEnabled = true

            }
        }

    }

}

使用BaseScrollViewController

let left = FirstController.init()
let middle = MiddleController()
let right = RightController.init()

let container = BaseScrollViewController.containerViewWith(left,middleVC: middle,rightVC: right)
container.maximumWidthFirstView = 150

输出

GitHub要点示例代码https://gist.github.com/mspvirajpatel/58dac2fae0d3b4077a0cb6122def6570

原文链接:https://www.f2er.com/iOS/332038.html

猜你在找的iOS相关文章