Swift-Mask使用

CALayer中的mask定义了父图层的部分可见区域,mask图层的 Color 属性是无关紧要的。mask图层实心的部分会被保留显示,其他的不显示.

Mask.gif

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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容