Ios UIView上遮罩的CABASICanization

Ios UIView上遮罩的CABASICanization,ios,swift,caanimation,Ios,Swift,Caanimation,我已经在我的视图中添加了一个遮罩,现在正在尝试设置它的动画。我已经尝试从中调整解决方案,但它不适合我。 我曾尝试在添加动画的位置切换,并删除setDisabledActions,但仍然得到相同的结果 extension UIView { public func mask(with rect: CGRect?, inverse: Bool = false, animated: Bool = true, duration: TimeInterval = 1.0) { guard l

我已经在我的视图中添加了一个遮罩,现在正在尝试设置它的动画。我已经尝试从中调整解决方案,但它不适合我。 我曾尝试在添加动画的位置切换,并删除setDisabledActions,但仍然得到相同的结果

extension UIView {
    public func mask(with rect: CGRect?, inverse: Bool = false, animated: Bool = true, duration: TimeInterval = 1.0) {

    guard let rect = rect else {
        self.layer.mask = nil
        return
    }

    let path = UIBezierPath(rect: rect)
    let maskLayer = CAShapeLayer()

    if inverse {
        path.append(UIBezierPath(rect: self.bounds))
        maskLayer.fillRule = .evenOdd
    }

    if animated {
        let animation = CABasicAnimation(keyPath: "path")
        animation.fromValue = maskLayer.path
        animation.toValue = path.cgPath
        animation.duration = duration
        animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        animation.isRemovedOnCompletion = false
        maskLayer.add(animation, forKey: "selectAnimation")
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        maskLayer.path = path.cgPath
        self.layer.mask = maskLayer
        CATransaction.commit()
    } else {
        maskLayer.path = path.cgPath
        self.layer.mask = maskLayer
    }

  }
}