Including a UIView frame animation problem. The view should be animated both in origin and in size, with an increase in size and a beginning moving linearly in order to maintain the view in the same place. But it happens that the view is reduced to size (0,0), and then enlarged to a size that is still not correct. See the attached video.
Problem video: https://media.pairby.com/I/u/a/IualExcJXn7CqLsGkcNZfwyEw5MKi3SV/v.mp4
func animateIn() {
let w = bounds.width
_iconView.frame = CGRect(
x: frame.midX - w/2,
y: frame.midY - w/2,
width: w, height: w)
isHidden = false
UIView.animate(withDuration: 0.2, animations: {
self.alpha = 1
let w = self.bounds.width * 0.5
self._iconView.frame = CGRect(
x: self.frame.midX - w/2,
y: self.frame.midY - w/2,
width: w, height: w)
})
}
func animateOut() {
UIView.animate(withDuration: 3, delay: 0, options: .beginFromCurrentState, animations: {
self.alpha = 0
let w = self.bounds.width
self._iconView.frame = CGRect(
x: self.frame.midX - w/2,
y: self.frame.midY - w/2,
width: w, height: w)
}, completion: { _ in self.isHidden = true })
}
More details:
self is a subclass of UIView limited to a superview.
_iconView is an UIImageView
animateIn guaranteed to work until animateOut
animateOutis a function that does not work properly, animateInworks