与UIView帧动画有关的问题.视图应在原点和大小两者中设置动画,大小增加,原点线性移动以使视图保持在同一位置.但是会发生什么,视图减小到大小(0,0),然后增加到仍然不正确的大小.见附件视频.
问题视频:https://media.pairby.com/I/u/a/IualExcJXn7CqLsGkcNZfwyEw5MKi3SV/v.mp4
func animateIn() { // Make _iconView large 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 // Animate it smaller let w = self.bounds.width * 0.5 self._iconView.frame = CGRect( x: self.frame.midX - w/2,y: self.frame.midY - w/2,height: w) }) } func animateOut() { UIView.animate(withDuration: 3,delay: 0,options: .beginFromCurrentState,animations: { self.alpha = 0 // Make it large again let w = self.bounds.width self._iconView.frame = CGRect( x: self.frame.midX - w/2,height: w) },completion: { _ in self.isHidden = true }) }
更多细节:
self是UIView的子类,仅限于superview.
_iconView是一个UIImageView
保证animateIn在animateOut之前运行
animateOut是不能按预期工作的函数,animateIn工作
解决方法
然而,还不完全清楚你需要做什么
>您无需更改大小即可 – 您可以简单地为_iconView框架设置当前状态的动画,以及
>因为_iconView是self的子视图,你需要相对于边界而不是框架定位它.
试试这样:
func doAnim() -> Void { UIView.animate(withDuration: 3,animations: { self.alpha = 0 let s = self.bounds.width let halfS = s / 2 self._iconView.frame = CGRect( x: self.bounds.midX - halfS,y: self.bounds.midY - halfS,width: s,height: s) }) }