美文网首页
iOS 引导页挖空透明

iOS 引导页挖空透明

作者: 秋叶红90 | 来源:发表于2020-11-19 23:31 被阅读0次
    1. 使用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 使用方法

    1. 添加属性
    @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
        }
    
    

    效果如下

    Simulator Screen Shot - iPhone 12 mini - 2020-11-19 at 23.27.33.png

    相关文章

      网友评论

          本文标题:iOS 引导页挖空透明

          本文链接:https://www.haomeiwen.com/subject/aeyriktx.html