【iOS】mask

//
//  ViewController.swift
//  Demo
//
//  Created by  on 2020/10/14.
//

import UIKit

class ViewController: UIViewController,CAAnimationDelegate {

    
    let layer = CAShapeLayer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(backImage)
        self.view.addSubview(iconImageView)
        self.animationWithView(view: iconImageView)
//        self.iconImageView.layer.mask = maskLayer
    }
    
    
    func animationWithView(view :UIImageView){
        
        
        let margin = 20;
        let radius = 25;
        
        let viewWidth = UIScreen.main.bounds.size.width
        let viewheight = UIScreen.main.bounds.size.height
        
        let endRadius = sqrt(viewheight*viewheight + viewWidth*viewWidth)
        
        let startRact = CGRect(x: Int(viewWidth) - margin - 2*radius, y: margin, width: 2*radius, height: 2*radius)
        let startPath = UIBezierPath(ovalIn: startRact)
        let endPath = UIBezierPath(ovalIn: CGRect.insetBy(startRact)(dx: -endRadius, dy: -endRadius))
        
        layer.fillColor = UIColor.red.cgColor
        layer.path = startPath.cgPath
        view.layer.mask = layer;
        
        
        let animation = CABasicAnimation(keyPath: "path")
        
        animation.fromValue = startPath.cgPath
        animation.toValue = endPath.cgPath
        animation.fillMode = CAMediaTimingFillMode.forwards
        animation.duration = 2
        animation.isRemovedOnCompletion = false
        animation.delegate = self
        layer.add(animation, forKey: "path")
        
        
    }
    
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
//        layer.removeAnimation(forKey: "path")
    }
    
    
    lazy var backImage : UIImageView = {
        let image = UIImageView(frame: self.view.bounds)
        image.image = UIImage(named: "adlogoOne.png")
        image.contentMode = UIView.ContentMode.scaleAspectFill
        return image
    }()
    
    
    
    lazy var iconImageView : UIImageView = {
        let image = UIImageView(frame: self.view.bounds)
        image.image = UIImage(named: "adlogoTwo.png")
        image.contentMode = UIView.ContentMode.scaleAspectFill
        image.backgroundColor = UIColor.red
        let pan = UITapGestureRecognizer(target: self, action: #selector(panAction(pan:)))
        image.addGestureRecognizer(pan)
        image.isUserInteractionEnabled = true
        return image
    }()

    
    
    
    
    
    
    @objc func panAction(pan:UITapGestureRecognizer){
        let point = pan.location(in: self.iconImageView)
        self.maskLayer.frame = CGRect(x: 0, y: 0, width: self.view.bounds.height, height: self.view.bounds.height)
        print(point)
    }
    
    
    lazy var maskLayer:CALayer = {
        let maskLayer = CALayer.init()
        maskLayer.frame = CGRect(x: 10, y: 20, width: 100, height: 100)
        maskLayer.cornerRadius = 50;
        maskLayer.backgroundColor = UIColor.red.cgColor
        return maskLayer
    }()

    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        
        
        
    }

}


在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值