说我有一个字符串数组,每次滑动时,我都希望屏幕上的文本是该数组中的一个随机字符串,而不会到达结尾。我该怎么办?
答案 0 :(得分:0)
否,您不能拥有无限滚动视图。 UIScrollView是矩形区域上的视图。该矩形区域可能很大,但有其局限性。
UIPageViewController
可能会做您想要的。自从我使用过一段时间以来,但是查看文档似乎可以提供视图控制器数组或提供符合dataSource
协议的UIPageViewControllerDataSource
。听起来您可以使用dataSource
来提供一组无限的包含字符串的视图控制器。
答案 1 :(得分:0)
您可以使用UIPageViewController
进行此操作。
这是一个简单的示例(将一个UIPageViewController
添加到您的情节提要,并将其类设置为InfinitePageViewController
):
//
// 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
}
}
这将无限滚动,为每个新页面从stringArray
中选择一个随机字符串。
因为这只是从数组中获取随机字符串,所以完全有可能多次获得相同的字符串-甚至依次获得。如果您不希望这样做,则一种方法是将索引编号的数组重新排列,然后逐步遍历该数组。当您到达末尾时,请重新洗牌并继续。