- 使用CAShapeLayer 然后使用UIBezierPath 换线条填充颜色黑色
let maskLayer = CAShapeLayer()
func configMask(rect:CGRect) {
let fromPath = maskLayer.path
maskLayer.fillColor = UIColor.black.cgColor
var radius:CGFloat = 5
let frame = rect
radius = min(radius, min(frame.width / 2.0, frame.height / 2.0))
let highlightedPath = UIBezierPath(roundedRect: frame, cornerRadius: radius)
let toPath = UIBezierPath(rect: bgView.bounds)
toPath.append(highlightedPath)
maskLayer.path = toPath.cgPath
/// CAShapeLayerFillRule.evenOdd 交叉挖空 另外一个是填充
maskLayer.fillRule = CAShapeLayerFillRule.evenOdd
bgView.layer.mask = maskLayer
let animation = CABasicAnimation(keyPath: "path")
animation.duration = 1
animation.fromValue = fromPath
animation.toValue = toPath
maskLayer.add(animation, forKey: nil)
}
2 使用方法
- 添加属性
@IBOutlet weak var bgMM: UIButton!
var bgView:TEEESAVie3w = Bundle.main.loadNibNamed("TEEESAVie3w", owner: nil, options: nil)!.first! as! TEEESAVie3w
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// let rect = CGRect.init(x: 0, y: 100.0, width: 100, height: 100)
//
bgView.frame = self.view.bounds
self.view.addSubview(bgView)
let rect = self.view.convert(self.bgMM.frame, from: self.bgMM.superview)
self.configMask(rect: rect)
self.view.backgroundColor = UIColor.red
}
效果如下

网友评论