在UIScrollView上创建一个循环 [英] Create a loop on UIScrollView

查看:63
本文介绍了在UIScrollView上创建一个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在视图上来回滚动,但不能循环视图,因此当我在ViewController3上时它会向后滚动,而在ViewController1上它会向前滚动以创建循环.我该怎么办?

I can scroll back and forth on the views but I can't loop the views so it scrolls back when I'm on ViewController3 and forward on ViewController1 to create a loop. How would I do this?

下面的代码是我到目前为止所做的.

The code below is what I've done so far.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    let vc0 = ViewController0(nibName: "ViewController0", bundle: nil)

    var frame0 = vc0.view.frame
    frame0.origin.x = self.view.frame.size.width
    vc0.view.frame = frame0

    self.addChildViewController(vc0)
    self.scrollView.addSubview(vc0.view)
    vc0.didMove(toParentViewController: self)

    let vc1 = ViewController1(nibName: "ViewController1", bundle: nil)

    var frame1 = vc1.view.frame
    frame1.origin.x = self.view.frame.size.width
    vc1.view.frame = frame1

    self.addChildViewController(vc1)
    self.scrollView.addSubview(vc1.view)
    vc1.didMove(toParentViewController: self)

    let vc2 = ViewController2(nibName: "ViewController2", bundle: nil)

    var frame2 = vc2.view.frame
    frame2.origin.x = self.view.frame.size.width * 2
    vc2.view.frame = frame2

    self.addChildViewController(vc2)
    self.scrollView.addSubview(vc2.view)
    vc2.didMove(toParentViewController: self)

    let vc3 = ViewController3(nibName: "ViewController3", bundle: nil)

    var frame3 = vc3.view.frame
    frame3.origin.x = self.view.frame.size.width * 3
    vc1.view.frame = frame3

    self.addChildViewController(vc3)
    self.scrollView.addSubview(vc3.view)
    vc3.didMove(toParentViewController: self)

    self.scrollView.contentSize = CGSize(width: Double(self.view.frame.size.width * 4), height: Double(self.view.frame.size.height - 66))

}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

推荐答案

实际上,我建议您完全放弃使用UIScrollView,而改为使用UIPageViewController.这使这种事情变得微不足道,因为您一次只需要提供一页,并且执行您描述的那种无限或循环滚动很容易,因为您每次都只需在循环系列中始终提供下一个"视图控制器即可.被要求.另外,它可以为您处理整个亲子舞蹈(不是您的操作方式有任何问题).

Actually I'd suggest you drop the use of UIScrollView altogether and use UIPageViewController instead. It makes this sort of thing trivial because you only have to supply one page at a time, and doing infinite or looping scrolling of the kind you describe is easy because you just keep supplying the "next" view controller in the looping series each time you are asked for it. Plus it handles the whole parent-child dance for you (not that there's anything wrong with how you're doing it).

这篇关于在UIScrollView上创建一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆