2

I want to make some thing like this with Replicator (image view and replicate that behind the imageview

so I used a xib file and put image view in that xib file I put the view(perspectiveView) and put the image view in that view and I want to replicate perspective view so here is my codes in UIView file that is xib file class

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)
    }
}
}

so this will work but not as I expected

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Saeed Rahmatolahi
  • 1,317
  • 2
  • 27
  • 60
  • nothing just receive this in log (lldb) when using this code the replicator will not done and when using replicator.addSublayer(perspectiveView.layer) the crash will happen – Saeed Rahmatolahi Dec 18 '18 at 07:51
  • I think you should remove this line `Bundle.main.loadNibNamed("perspectiveView", owner: self, options: nil)` from `perspectiveView`. Instead set `perspectiveView` as custom class name in `Xib` file. Then use that line `Bundle.main.loadNibNamed` to load this view wherever you want to use it but not inside the same `class`. – Kamran Dec 18 '18 at 07:58
  • please check the updated codes this will work but not as I expected – Saeed Rahmatolahi Dec 18 '18 at 08:18

0 Answers0