I have a UICollectionView with cells containing WKWebViews. When I scroll through these cells there is a lag because the WKWebViews aren't cached. Is there some way to cache the contents of the WKWebViews to prevent this lag?
For reference I am using the YouTubePlayer-Swift Cocoapod and this is my custom UICollectionViewCell:
import UIKit
import YouTubePlayer_Swift
class YouTubeVideoCollectionViewCell: UICollectionViewCell {
let borderView = UIView(frame: .zero)
var videoView = YouTubePlayerView(frame: .zero)
var video: YouTubeVideo? {
didSet {
UISetUp()
}
}
override open func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
self.updateConstraintsIfNeeded()
return super.preferredLayoutAttributesFitting(layoutAttributes)
}
override func layoutSubviews() {
super.layoutSubviews()
changeToShadowedCard()
self.contentView.layoutIfNeeded()
}
func changeToShadowedCard() {
backgroundColor = .clear
contentView.addSubview(borderView)
borderView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
borderView.topAnchor.constraint(equalTo: self.contentView.topAnchor),
borderView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor),
borderView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor),
borderView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor),
])
borderView.layer.masksToBounds = true
borderView.backgroundColor = UIColor.secondarySystemBackground
borderView.layer.cornerRadius = 5
borderView.addShadow(shadowColor: UIColor.label.cgColor, shadowOffset: CGSize(width: 0, height: 0), shadowOpacity: 0.6, shadowRadius: 3)
}
func UISetUp() {
guard let id = video?.id else { return }
borderView.addSubview(videoView)
videoView.loadVideoID(id)
videoView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
videoView.topAnchor.constraint(equalTo: borderView.topAnchor),
videoView.bottomAnchor.constraint(equalTo: borderView.bottomAnchor),
videoView.rightAnchor.constraint(equalTo: borderView.rightAnchor),
videoView.leftAnchor.constraint(equalTo: borderView.leftAnchor),
])
videoView.clipsToBounds = true
videoView.layer.cornerRadius = 5
}
}