I'm creating inside my app this customized view that can be expanded by the user with a drag and has all the animations of the case. Just to be clear, I want to reproduce pretty much the same animation of the control center of IOS. So far I managed to obtain pretty much everything from the animation of the view's expansion when the finger drags it, to the possibility of make the corners rounded.
Now, the fact is that, besides the possibility to drag the view, I want to implement an animation when the user takes off the finger in between the expansion in order to make the view come back to its original height or finish the expansion. To do that I'm using the position of the view when the user stops dragging.
The issues started when I tried to animate the UIView.layer.mask
. With some research on the internet, I discovered the CABasicAnimation
class and I implemented it inside a function:
func animateExpandableView(withDuration duration: Double) {
CATransaction.begin()
CATransaction.setAnimationDuration(duration)
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: .easeInEaseOut))
let maskAnimation = CABasicAnimation(keyPath: "mask")
maskAnimation.fromValue = profileView.layer.mask
let bounds = profileView.frame
let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 8.0, height: 8.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = bounds
maskLayer.path = maskPath.cgPath
maskAnimation.toValue = maskLayer
maskAnimation.duration = duration
profileView.layer.mask = maskLayer
profileView.layer.add(maskAnimation, forKey: "mask")
CATransaction.commit()
}
Btw this doesn't work and the change of the mask isn't animated. Where did I make a mistake in the implementation?
Someone suggested to me to check this link from another question; I didn't find it very useful because, despite the fact it didn't work for my case, the answer is written in C and not in Swift.