遇到 UIView 帧动画问题。 View 应该在原点和大小上都具有动画效果,随着大小增加和原点线性移动以保持 View 在同一位置。但是发生的事情是, View 减小到大小 (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,
width: w, 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,
y: self.frame.midY - w/2,
width: w, height: w)
}, completion: { _ in self.isHidden = true })
}
更多详情:
self
是 UIView 的子类,受限于父 View 。
_iconView
是一个 UIImageView
animateIn
保证在 animateOut
animateOut
是未按预期工作的函数,animateIn
工作
最佳答案
您还不完全清楚您需要做什么,但是...
开始时您不需要更改大小 - 您可以简单地将 _iconView 框架从其当前状态设置为动画,并且
因为 _iconView 是
self
的 subview ,您需要相对于边界而不是框架定位它。
像这样尝试:
func doAnim() -> Void {
UIView.animate(withDuration: 3, delay: 0, options: .beginFromCurrentState, 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)
})
}
关于ios - UIImageView 框架没有按预期进行动画处理(大小和原点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46078196/