在swift 4中是否可以为页面控制提供无限水平分页uiscrollview?

在swift 4中是否可以为页面控制提供无限水平分页uiscrollview?,swift,uiscrollview,uipagecontrol,Swift,Uiscrollview,Uipagecontrol,假设我有一个字符串数组,每次我滑动时,我希望屏幕上的文本是数组中的随机字符串,而不会到达结尾。我该怎么做呢?不,你不能有无限滚动视图。UIScrollView是矩形区域上的视图。这个矩形区域可以很大,但有限制 UIPageViewController可能会执行您想要的操作。我已经有一段时间没有使用它了,但是从文档来看,您可以提供一个视图控制器数组,或者提供符合UIPageViewControllerDataSource协议的dataSource。听起来您可以使用数据源来提供包含字符串的无限多个视

假设我有一个字符串数组,每次我滑动时,我希望屏幕上的文本是数组中的随机字符串,而不会到达结尾。我该怎么做呢?

不,你不能有无限滚动视图。UIScrollView是矩形区域上的视图。这个矩形区域可以很大,但有限制


UIPageViewController
可能会执行您想要的操作。我已经有一段时间没有使用它了,但是从文档来看,您可以提供一个视图控制器数组,或者提供符合
UIPageViewControllerDataSource
协议的
dataSource
。听起来您可以使用
数据源
来提供包含字符串的无限多个视图控制器。

您可以使用
UIPageViewController
来实现这一点

下面是一个简单的示例(将
UIPageViewController
添加到情节提要中,并将其类设置为
InfinitePageViewController
):

这将无限滚动,为每个新页面从
stringArray
中选择一个随机字符串


因为这只是从数组中获取一个随机字符串,所以完全有可能多次获取相同的字符串,甚至是按顺序。如果您不想这样做,一种方法是将索引号数组洗牌,然后逐步遍历该数组。当您到达终点时,再次洗牌数字并继续。

实际上,您可以使用
UIScrollView
创建一个无限滚动条。不过需要一些技巧。它类似于实现自定义UITableView,但需要将contentOffset设置为contentSize的中心,而不使用动画,并模拟scrollView所具有的动画。我编写了一个快速页面查看控制器演示应用程序,并试图在下班后将其推送到GitHub,但我们的办公室安全部门出于某种原因阻止了我。一个小问题是:如果你洗牌你的数组,走到最后,然后再次洗牌,如果一次洗牌的最后一个条目与新洗牌的第一个条目相同,你仍然可以得到一个重复条目。我创建了一个类“RandomItems”,它提供数组中的随机项,并避免了这种情况。。。在最后洗牌肯定会导致重复项目。。。你会想处理这个案子的。当然,OP要求“无限随机显示有限数组中的字符串”-因此,仅使用该指令,在一行中显示相同字符串5次是有效的。几乎肯定不是他想的:)我在Github上的项目RandomObjects解决了“最后重复”的问题:
//
//  InfinitePageViewController.swift
//  InfinitePages
//
//  Created by Don Mag on 10/30/18.
//

import UIKit

// simple random color extension
extension UIColor {
    static func randomColor(saturation: CGFloat = 1, brightness: CGFloat = 1, alpha: CGFloat = 1) -> UIColor {
        let hue = CGFloat(arc4random_uniform(361)) / 360.0
        return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: alpha)
    }
}

class SinglePageViewController: UIViewController {

    var theLabel: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .white
        v.textAlignment = .center
        v.numberOfLines = 0
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.randomColor()

        view.addSubview(theLabel)
        NSLayoutConstraint.activate([
            theLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 40.0),
            theLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -40.0),
            theLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 40.0),
            theLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -40.0),
            ])

    }

}

class InfinitePageViewController: UIPageViewController, UIPageViewControllerDataSource {

    var stringArray = [
        "1 - Do not consider painful what is good for you.",
        "2 - It is better to look ahead and prepare than to look back and regret.",
        "3 - The easiest thing in the world to be is you. The most difficult thing to be is what other people want you to be. Don't let them put you in that position.",
        "4 - A book is a version of the world. If you do not like it, ignore it; or offer your own version in return.",
        "5 - The scientific name for an animal that doesn't either run from or fight its enemies is lunch.",
        "6 - To love is to receive a glimpse of heaven.",
        "7 - Patriotism is your conviction that this country is superior to all other countries because you were born in it.",
        "8 - There is but one temple in the universe and that is the body of man.",
        "9 - It is easier to get forgiveness than permission.",
        "10 - There is always more misery among the lower classes than there is humanity in the higher.",
        "11 - Success is counted sweetest by those who ne'er succeed.",
        "12 - Indifference and neglect often do much more damage than outright dislike.",
        "13 - For what do we live, but to make sport for our neighbours, and laugh at them in our turn?",
        "14 - Last night somebody broke into my apartment and replaced everything with exact duplicates... When I pointed it out to my roommate, he said, 'Do I know you?'",
        "15 - Do not weep; do not wax indignant. Understand.",
        "16 - Work saves us from three great evils: boredom, vice and need.",
        "17 - You can discover what your enemy fears most by observing the means he uses to frighten you.",
        "18 - Rest satisfied with doing well, and leave others to talk of you as they please.",
        "19 - People want economy and they will pay any price to get it.",
        "20 - On the whole human beings want to be good, but not too good, and not quite all the time.",
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataSource = self

        // create the first "page"
        let vc = SinglePageViewController()
        vc.theLabel.text = stringArray[0]

        // set it as the initial page VC
        self.setViewControllers([vc], direction: .forward, animated: false, completion: nil)

    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        // don't allow scrolling backward
        return nil
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let vc = SinglePageViewController()
        let randomIndex = Int(arc4random_uniform(UInt32(stringArray.count)))
        vc.theLabel.text = stringArray[randomIndex]
        return vc
    }

}