在swift 4中,是否可以有一个用于页面控制的无限水平分页界面



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

不,您不能拥有无限滚动视图。UIScrollView是一个矩形区域的视图。这个矩形区域可以很大,但也有局限性。

UIPageViewController可能会做你想做的事。我已经有一段时间没有使用它了,但从文档来看,您可以提供一个视图控制器阵列,也可以提供符合UIPageViewControllerDataSource协议的dataSource。听起来您可以使用dataSource来提供一组包含字符串的无限视图控制器。

您可以使用UIPageViewController执行此操作。

这里有一个简单的例子(将UIPageViewController添加到故事板中,并将其Class设置为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中随机选择一个字符串。

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

相关内容

  • 没有找到相关文章

最新更新