CALayer中的mask定義了父圖層的部分可見區(qū)域泼橘,mask圖層的 Color 屬性是無關(guān)緊要的涝动。mask圖層實心的部分會被保留顯示,其他的不顯示.
UI代碼:
<pre><code>` let imgFrame:CGRect = CGRect.init(x: 100, y: 200, width: 100, height: 100)
self.backImgView = UIImageView.init(frame: imgFrame)
self.backImgView.image = UIImage.init(named: "animal")?.grayImage()
self.backImgView.contentMode = UIViewContentMode.scaleAspectFit;
self.view.addSubview(self.backImgView)
self.frontImgView = UIImageView.init(frame: imgFrame)
self.frontImgView.image = UIImage.init(named: "animal")
self.frontImgView.contentMode = UIViewContentMode.scaleAspectFit;
self.view.addSubview(self.frontImgView)
self.masklayer = CAShapeLayer()
self.masklayer.frame = CGRect.init(x: 0, y: 0, width: 100, height: 100)
self.masklayer.position = CGPoint(x: 50, y: 150)
self.masklayer.backgroundColor = UIColor.white.cgColor
self.frontImgView.layer.mask = self.masklayer`</code></pre>
測試代碼:
<pre><code>` @IBAction func beginAnimation(_ sender: UIButton) {
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
self.masklayer.position = CGPoint(x: 50, y: 50)
}) { (isfinished) in
}
}`</code></pre>