所以我使用了xib文件并将图像视图放入该xib文件中,然后将view(perspectiveView)放入图像视图中,我想复制透视图,所以这是我在UIView文件中的代码,即xib文件类
import UIKit
class perspectiveView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var perspectiveTitle: UILabel!
@IBOutlet weak var perspectiveView: UIView!
@IBOutlet weak var perspectiveImage: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
override func layoutSubviews() {
super.layoutSubviews()
let replicatorLayer = CAReplicatorLayer()
let coloredSquare = CALayer()
// coloredSquare.backgroundColor = UIColor.red.cgColor
coloredSquare.frame = perspectiveView.bounds
coloredSquare.contents = perspectiveView.asImage().cgImage
let instanceCount = 3
replicatorLayer.instanceCount = instanceCount
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(5, -10, -1)
replicatorLayer.instanceAlphaOffset = -0.3
replicatorLayer.addSublayer(coloredSquare)
perspectiveView.layer.addSublayer(replicatorLayer)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
Bundle.main.loadNibNamed("perspectiveView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight , .flexibleWidth]
}
}
extension UIView {
// Using a function since `var image` might conflict with an existing variable
// (like on `UIImageView`)
func asImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}
所以这将起作用,但不像我预期的那样