I have added a mask to my view and am now trying to animate it. I have attempted to adapt the solution from https://stackoverflow.com/a/36461202/642502 but it's not animating for me. I have tried to swap around where the animation is added and removing the setDisabledActions but continue to get the same results.
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
}
}
}