动画完成后执行转场
Posted
技术标签:
【中文标题】动画完成后执行转场【英文标题】:Performing a segue after animation is finished 【发布时间】:2016-05-12 04:13:02 【问题描述】:我正在尝试在。这是我在 viewDidAppear 中的代码:
override func viewDidAppear(animated: Bool)
super.viewDidAppear(animated)
imageViewAnimated.startAnimating()
if label.center != CGPoint(x:50, y:10)
UIView.animateWithDuration(2.0, delay: 0.0, usingSpringWithDamping: 0.0, initialSpringVelocity: 0.0, options: .CurveEaseOut, animations: () -> Void in
self.label.center = self.view.center
, completion: nil)
UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.0, initialSpringVelocity: 0.0, options: .CurveEaseOut, animations: () -> Void in
// self.label.center = CGPoint(x: 50,y:300)
self.label.alpha = 0.0
, completion: nil)
if label.alpha == 0.0
UIView.animateWithDuration(0.5, delay: 10.0, usingSpringWithDamping: 0.0, initialSpringVelocity: 0.0, options: .CurveEaseOut, animations: () -> Void in
self.performSegueWithIdentifier("backSegue", sender: self)
, completion: nil)
所以基本上我有一个向下移动和淡出的标签(第一个和第二个 animateWithDuration),然后我希望在标签淡出并且其他动画完成后发生 Segue。现在,segue 正在发生,但它会立即执行,并且无论我增加多少延迟都不会等待。
【问题讨论】:
如果您想在功能完成后执行任何操作,那么您必须在“完成”块上对其进行编码。在方法完成其工作后将调用完成块。 我已经更新了答案只是检查 【参考方案1】:你在错误的地方调用了segue。你想要这样的东西:
override func viewDidAppear(animated: Bool)
super.viewDidAppear(animated)
imageViewAnimated.startAnimating()
if label.center != CGPoint(x:50, y:10)
UIView.animateWithDuration(2.0, delay: 0.0, usingSpringWithDamping: 0.0, initialSpringVelocity: 0.0, options: .CurveEaseOut, animations: () -> Void in
self.label.center = self.view.center
, completion: nil)
UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.0, initialSpringVelocity: 0.0, options: .CurveEaseOut, animations: () -> Void in
// self.label.center = CGPoint(x: 50,y:300)
self.label.alpha = 0.0
, completion: finished in
self.performSegueWithIdentifier("backSegue", sender: self)
)
【讨论】:
感谢它的工作!我不知道为什么,但它需要一段时间(大约 15 秒来做 segue)。可能只是我的电脑太慢了。【参考方案2】:完成后运行你的 segue 代码。您必须在“完成”块上对其进行编码。方法完成动画后将调用完成块
UIView.animateWithDuration(0.5, delay: 10.0, usingSpringWithDamping: 0.0, initialSpringVelocity: 0.0, options: .CurveEaseOut, animations: () -> Void in
//if you perform segue here if will perform with animation
, completion: finished in
//if you perform segue it will perform after it finish animating.
self.performSegueWithIdentifier("backSegue", sender: self)
)
【讨论】:
【参考方案3】:您可以在动画块的完成处理程序部分编写导航代码,因为完成处理程序将在动画完成后触发。
[UIView animateWithDuration:0.25 animations:^
//Animation changes
completion:^(BOOL finished)
//Navigation
];
【讨论】:
请注意,该问题被标记为 Swift,而不是 Objective-C。以上是关于动画完成后执行转场的主要内容,如果未能解决你的问题,请参考以下文章