CALayer中的mask定义了父图层的部分可见区域,mask图层的 Color 属性是无关紧要的。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>