xcode iOS Swift Animation - 将图像移动到新位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26004765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS Swift Animation - Move image to the new position
提问by ekussberg
I am trying to implement this cool feature where the logo after the app loads moves up slightly and the login view appears. The problem is that when I use this code:
我正在尝试实现这个很酷的功能,其中应用程序加载后的徽标略微向上移动并出现登录视图。问题是当我使用此代码时:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
UIView.animateWithDuration(2, animations: {
var newCenter = self.logoImage.center
newCenter.y -= 100
self.logoImage.center = newCenter
}, completion: { finished in
println("Basket doors opened!")
})
}
The logo is moved to the origin place from the place I want it to move to. So in this example logo appears already with (y-100) and then moves to the origin X,Y.
徽标从我希望它移动到的地方移动到原点。所以在这个例子中,标志已经和 (y-100) 一起出现,然后移动到原点 X,Y。
回答by sfeuerstein
Since the view isn't actually laid out / on the screen in viewDidLoad()
you shouldn't be trying to animate anything there. Instead, you should wait until the view is completely presented, which happens in viewDidAppear(animated: Bool)
. Also, I would recommend calculating the newCenter
var outside of the animation call, then simply setting self.logoImage.center = newCenter
within the animation call.
由于视图实际上并未在屏幕上布置/显示,因此viewDidLoad()
您不应该尝试在其中设置任何动画。相反,您应该等到视图完全呈现,这发生在viewDidAppear(animated: Bool)
. 另外,我建议newCenter
在动画调用之外计算var,然后在动画调用中简单地设置self.logoImage.center = newCenter
。