iOS - 动画背景问题
Posted
技术标签:
【中文标题】iOS - 动画背景问题【英文标题】:iOS - Animating background issue 【发布时间】:2017-12-05 11:35:48 【问题描述】:我正在尝试为屏幕上的几个元素设置动画,代码如下:
private func setupAnimations()
self.emailAddress.alpha = 0
self.fullName.alpha = 0
self.password.alpha = 0
self.confirmPassword.alpha = 0
self.signUp.alpha = 0
self.fullName.leftView?.alpha = 0
self.emailAddress.leftView?.alpha = 0
self.password.leftView?.alpha = 0
self.confirmPassword.leftView?.alpha = 0
self.emailAddress.center.x += self.view.bounds.width-10
self.fullName.center.x -= self.view.bounds.width-10
self.password.center.x -= self.view.bounds.width-10
self.confirmPassword.center.x += self.view.bounds.width-10
self.signUp.center.y += self.view.bounds.height/2
UIView.animate(withDuration: 1.7, delay: 0.2, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [.allowUserInteraction,.curveEaseInOut], animations:
self.emailAddress.alpha = 1
self.emailAddress.center.x -= self.view.bounds.width-10
self.view.layoutIfNeeded()
, completion: (completed) in
if completed
UIView.animate(withDuration: 1, animations:
self.fullName.leftView?.alpha = 1
self.emailAddress.leftView?.alpha = 1
self.password.leftView?.alpha = 1
self.confirmPassword.leftView?.alpha = 1
)
)
UIView.animate(withDuration: 1.7, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [.allowUserInteraction,.curveEaseInOut], animations:
self.fullName.alpha = 1
self.fullName.center.x += self.view.bounds.width-10
self.view.layoutIfNeeded()
, completion: (completed) in
if completed
UIView.animate(withDuration: 1, animations:
self.fullName.leftView?.alpha = 1
self.emailAddress.leftView?.alpha = 1
self.password.leftView?.alpha = 1
self.confirmPassword.leftView?.alpha = 1
)
)
等等......你明白了 - 我将 alpha 设置为 0 并将 x 居中设置为离屏,并为其“到达”屏幕设置动画,在 viewDidLoad() 中调用它。一切都解决了一个月,但最近我检查了这个问题:
视频链接:https://streamable.com/lhqvu
作为超级视图中的 UIImage 的背景也正在被动画化,从而可以看到超级视图(红色背景)。
任何帮助将不胜感激。提前谢谢你。
【问题讨论】:
在 viewDidAppear 方法中尝试所有动画,将您的代码分成两部分在视图中执行是否加载 1)初始设置,您设置所有 alpha 0 和您设置的任何帧 2)(在视图中执行确实出现了)应用动画 【参考方案1】:在viewWillAppear
中添加您的动画setupAnimations
方法,并从usingSpringWithDamping
块中删除self.view.layoutIfNeeded()
。在情节提要中取消选中 safeArea 并在将顶部约束设置为 superview 时取消选中 Constrain to margins 并设置为零。
【讨论】:
【参考方案2】:当涉及到动画时,您应该调用 viewDidAppear
上的函数,而不是 viewDidLoad。
【讨论】:
以上是关于iOS - 动画背景问题的主要内容,如果未能解决你的问题,请参考以下文章