【问题标题】:Move scrollview automatically with page controller使用页面控制器自动移动滚动视图
【发布时间】:2019-09-08 09:27:08
【问题描述】:

您好,我在我的应用程序中使用PageController

我已经在scrollView上插入了所有数据。

我想要做的是,我想在特定时间自动移动scrollview,就像页面控件一样。

scrollView 到达最后一页时,它会再次快速调用所有页面并从第一页开始。

我想在最后一页完成后非常顺利地加载/获取第一页。

通过以下方式查找我的代码。

var slides:[Slide] = [];
var offSet: CGFloat = 0
override func viewDidLoad() {
    super.viewDidLoad()

    slides = createSlides()
    setupSlideScrollView(slides: slides)

    pageControl.numberOfPages = slides.count
    pageControl.currentPage = 0
    view.bringSubview(toFront: pageControl)

    let timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(autoScroll), userInfo: nil, repeats: true)

}

@objc func autoScroll() {
    let totalPossibleOffset = CGFloat(slides.count - 1) * self.view.bounds.size.width
    if offSet == totalPossibleOffset {
        offSet = 0 // come back to the first image after the last image
    }
    else {
        offSet += self.view.bounds.size.width
    }
    DispatchQueue.main.async() {
        UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
            self.scrollView.contentOffset.x = CGFloat(self.offSet)
        }, completion: nil)
    }
}

【问题讨论】:

  • 我已经在这里看到了,但是我在这里感到困惑 let subView = UIView(frame: frame) subView.backgroundColor = slides[index] 我需要在这里显示幻灯片数组
  • 所以你只需将图像视图添加为子视图而不是 Uiview
  • @srikanthkumar 请将术语 Pagecontroller 替换为 page controlUIPageControl 控制器与控件不同。

标签: ios swift scroll uipagecontrol


【解决方案1】:
@objc func autoScroll() {
    let totalPossibleOffset = CGFloat(slides.count - 1) * self.view.bounds.size.width
    if offSet == totalPossibleOffset {
        offSet = 0 // come back to the first image after the last image
    }
    else {
        offSet += self.view.bounds.size.width
    }
    DispatchQueue.main.async() {
        UIView.animate(withDuration: 0.1, delay: 0, options: UIView.AnimationOptions.curveLinear, animations: {
            self.scrollView.scroll(topOffset:offSet, animated: true)
        }, completion: nil)
    }
}


extension UIScrollView {

    // Bonus: Scroll
    func scroll(topOffset:Float, animated: Bool) {
        let ofSetValue = CGPoint(x: topOffset, y: 0)
        setContentOffset(ofSetValue, animated: animated)
    }
}

【讨论】:

  • 它会给出如下错误:无法将'CGFloat'类型的值转换为预期的参数类型'CGPoint'
【解决方案2】:

使用以下代码可以正常工作。

 @objc func autoScroll() {
        let totalPossibleOffset = CGFloat(slides.count - 1) * self.view.bounds.size.width
        if offSet == totalPossibleOffset {
            offSet = 0 // come back to the first image after the last image
        }
        else {
            offSet += self.view.bounds.size.width
        }
        DispatchQueue.main.async() {
            UIView.animate(withDuration: 0.0, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
                self.scrollView.contentOffset.x = CGFloat(self.offSet)
            }, completion: nil)
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 2017-05-12
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多