我正在使用此代码来创建简单的阅读器。在我的阅读器中,我有用于显示图像的图像视图和用于在屏幕上显示文本的文本标签。一切工作正常,但是当我启动应用程序并开始翻页时,在下一页出现之前我遇到了几秒钟的轻微延迟。但如果我向前滚动所有页面。然后我将向后滚动所有页面。我将再次开始向前翻阅页面。那么这几秒钟的延迟就会消失。有没有办法解决这个延迟?也许我需要以某种方式预加载图像?
读卡器控制器
class ReaderController: UIViewController {
var pagesData = [PageData]()
var index = Int()
var pageIndex: Int = -1
let pageContainer: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let pageViews: [PageLayout] = {
let view = [PageLayout(), PageLayout()]
view[0].translatesAutoresizingMaskIntoConstraints = false
view[1].translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
setupConstraints()
pageViews[0].index = index
pageViews[1].index = index
pageViews[0].pageIndex = pageIndex
pageViews[1].pageIndex = pageIndex
pageTransition(animated: false, direction: "fromRight")
}
func setupViews() {
pageContainer.addSubview(pageViews[0])
pageContainer.addSubview(pageViews[1])
view.addSubview(pageContainer)
}
func setupConstraints() {
pageContainer.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0).isActive = true
pageContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0).isActive = true
pageContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0).isActive = true
pageContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0).isActive = true
pageViews[0].topAnchor.constraint(equalTo: pageContainer.topAnchor).isActive = true
pageViews[0].bottomAnchor.constraint(equalTo: pageContainer.bottomAnchor).isActive = true
pageViews[0].leadingAnchor.constraint(equalTo: pageContainer.leadingAnchor).isActive = true
pageViews[0].trailingAnchor.constraint(equalTo: pageContainer.trailingAnchor).isActive = true
pageViews[1].topAnchor.constraint(equalTo: pageContainer.topAnchor).isActive = true
pageViews[1].bottomAnchor.constraint(equalTo: pageContainer.bottomAnchor).isActive = true
pageViews[1].leadingAnchor.constraint(equalTo: pageContainer.leadingAnchor).isActive = true
pageViews[1].trailingAnchor.constraint(equalTo: pageContainer.trailingAnchor).isActive = true
}
func loadData(fileName: Any) -> PagesData {
var url = NSURL()
url = Bundle.main.url(forResource: "text", withExtension: "json")! as NSURL
let data = try! Data(contentsOf: url as URL)
let person = try! JSONDecoder().decode(PagesData.self, from: data)
return person
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: view.self)
if view.safeAreaInsets.left > 30 {
if (location.x > self.view.frame.size.width - (view.safeAreaInsets.left * 1.5)) {
pageTransition(animated: true, direction: "fromRight")
} else if (location.x < (view.safeAreaInsets.left * 1.5)) {
pageTransition(animated: true, direction: "fromLeft")
}
}
else {
if (location.x > self.view.frame.size.width - 40) {
pageTransition(animated: true, direction: "fromRight")
} else if (location.x < 40) {
pageTransition(animated: true, direction: "fromLeft")
}
}
}
}
func pageTransition(animated: Bool, direction: String) {
let result = loadData(fileName: pagesData)
switch direction {
case "fromRight":
pageIndex += 1
case "fromLeft":
pageIndex -= 1
default: break
}
pageViews[0].pageIndex = pageIndex
pageViews[1].pageIndex = pageIndex
if pageIndex <= -1 {
pageIndex = 0
} else if pageIndex >= result.pagesData.count {
pageIndex = result.pagesData.count - 1
} else {
let fromView = pageViews[0].isHidden ? pageViews[1] : pageViews[0]
let toView = pageViews[0].isHidden ? pageViews[0] : pageViews[1]
toView.configure(theData: result.pagesData[pageIndex])
fromView.isHidden = true
toView.isHidden = false
}
}
}
页面布局
class PageLayout: UIView {
var index = Int()
var pageIndex = Int()
private let imageView: UIImageView = {
let image = UIImageView()
image.contentMode = .scaleAspectFill
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()
var imageViewTopConstraint = NSLayoutConstraint()
var imageViewBottomConstraint = NSLayoutConstraint()
var imageViewLeadingConstraint = NSLayoutConstraint()
var imageViewTrailingConstraint = NSLayoutConstraint()
private let textLabel: UILabel = {
let label = UILabel()
label.font = UIFont(name: "ComicSansMS-Bold", size: 19)
label.textColor = .white
label.textAlignment = .center
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
label.layer.shadowColor = UIColor.black.cgColor
label.layer.shadowOffset = CGSize.zero
label.layer.shadowOpacity = 1.0
label.layer.shadowRadius = 4
return label
}()
var textLabelTopConstraint = NSLayoutConstraint()
var textLabelBottomConstraint = NSLayoutConstraint()
var textLabelLeadingConstraint = NSLayoutConstraint()
var textLabelTrailingConstraint = NSLayoutConstraint()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(imageView)
addSubview(textLabel)
setupConstraints()
}
required init?(coder: NSCoder) {
fatalError("Not happening")
}
func setupConstraints() {
imageViewTopConstraint = imageView.topAnchor.constraint(equalTo: topAnchor, constant: 0.0)
imageViewBottomConstraint = imageView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0.0)
imageViewLeadingConstraint = imageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0.0)
imageViewTrailingConstraint = imageView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0.0)
addConstraints([imageViewTopConstraint, imageViewBottomConstraint, imageViewLeadingConstraint, imageViewTrailingConstraint])
textLabelTopConstraint = imageView.topAnchor.constraint(equalTo: topAnchor, constant: 0.0)
textLabelBottomConstraint = imageView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0.0)
textLabelLeadingConstraint = imageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0.0)
textLabelTrailingConstraint = imageView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0.0)
addConstraints([textLabelTopConstraint, textLabelBottomConstraint, textLabelLeadingConstraint, textLabelTrailingConstraint])
}
func configure(theData: PageData) {
textLabel.text = theData.textData
imageView.image = UIImage(named: "page\(pageIndex+1)")
}
}
为了消除翻页时的延迟,您需要预加载图像,这样每次翻页时就不必再次加载它们。一种方法是使用图像缓存。
这是一个如何实现缓存的简单示例:
在ReaderController中添加缓存属性:
var imageCache = NSCache<NSNumber, UIImage>()
更新PageLayout类中的configure方法,从缓存中加载图片或者将图片添加到缓存中:
func configure(theData: PageData) {
textLabel.text = theData.textData
let imageKey = NSNumber(value: pageIndex)
if let cachedImage = imageCache.object(forKey: imageKey) {
imageView.image = cachedImage
} else {
if let image = UIImage(named: "page\(pageIndex+1)") {
imageCache.setObject(image, forKey: imageKey)
imageView.image = image
}
}
}
现在,图像只会加载一次并存储在缓存中,显着加快翻页时的显示速度。