Ios Swift:removeFromSubview发生在动画之前

Ios Swift:removeFromSubview发生在动画之前,ios,animation,swift,jquery-animate,Ios,Animation,Swift,Jquery Animate,我有一个小设置,其中我有一个MPMoviePlayerController设置作为视频背景。我试图让它在更新/更改时平稳过渡。这是我目前的密码。顺便说一句,newBackground.view.alpha最初为0 let oldBackground = currentBackground self.view.insertSubview(newBackground.view, aboveSubview: oldBackground.view) UIView.animateWithDurati

我有一个小设置,其中我有一个MPMoviePlayerController设置作为视频背景。我试图让它在更新/更改时平稳过渡。这是我目前的密码。顺便说一句,newBackground.view.alpha最初为0

let oldBackground = currentBackground

self.view.insertSubview(newBackground.view, aboveSubview: oldBackground.view)
  UIView.animateWithDuration(1.5 as NSTimeInterval, animations: {
    newBackground.view.alpha = 1
    }, completion: {
      finished in
      oldBackground.view.removeFromSuperview()
      println("The subview should be removed now")
  })
执行此操作时,oldBackground.view会在新背景开始淡入之前立即删除。但是,println发生在动画完成之后。我不明白为什么removeFromSuperview会立即发生,但println会在我期望的时候发生。如果我删除
oldBackground.view.removeFromSuperview()
,动画会淡入并看起来很好(但是视图显然没有被删除,它只是在新背景后面)

编辑:奇怪的是,它似乎在模拟器中按预期工作。在我的iphone6plus上运行每次都会给我带来问题。我已从Xcode卸载并重新运行它,但问题仍然存在。


如果有人对我有什么建议,我会很高兴的。谢谢。

我猜在动画过程中执行
newBackground.view.alpha=1时,它会使背景可见并覆盖旧背景。

将新子视图放在旧子视图上方时,旧子视图会放在后面。这发生在动画代码开始之前。插入新子视图之前,请确保其alpha为0。如果你想要一个淡出的动画,你应该将你的OldBackgroundAlpha设置为0

像这样:

let oldBackground = currentBackground

newBackground.view.alpha = 0

self.view.insertSubview(newBackground.view, aboveSubview: oldBackground.view)
  UIView.animateWithDuration(1.5 as NSTimeInterval, animations: {
    newBackground.view.alpha = 1
    oldBackground.view.alpha = 0
    }, completion: {
      finished in
      oldBackground.view.removeFromSuperview()
      println("The subview should be removed now")
  })

附录:在
完成中
检查bool是否在末尾。Thomas Kilian:即使我检查bool(甚至检查确保alpha在动画结束时应该在哪里),它仍然会立即执行。该println仅在动画完成后发生一次,但removeFromSuperview立即发生。你看到模拟器工作正常的部分了吗?这只是一个猜测。对不起,这没用。也许还有其他人。谢谢你的意见。我在原始问题中提到“newBackground.view.alpha最初是0”。我当前的设置应该是在旧背景前面添加一个透明的新背景。新背景动画设置为alpha=1(因此完全隐藏旧背景,使其清晰地消失)。这在模拟器中非常有效,但在我的iPhone6Plus上会闪烁黑色。还有其他想法吗?你试过改变时间间隔吗?我会尝试不同的时间,看看它是否会影响到任何东西。澄清一下,在我的例子中,“闪烁黑色”意味着在动画之前删除旧的background.view,露出我的黑色主视图。塞尔达:时间间隔似乎没有什么区别。视图立即被删除,然后在动画完成后进行println。我刚刚理解了这个问题。我以为它直接展示了新的视角。我现在没有任何想法,但如果我想到任何东西,我会再写一次。这是一个很长的镜头,我只想把它放在评论中:旧视频还在播放还是在它转换时结束了?您使用的是相同的电影播放器控制器实例吗?您是淡出还是将旧版本的alpha设置为0?这可能与iOS 8.1.2有关?在两个不同的物理iphone(iOS 8.1.2上的6和6 Plus)上进行测试会导致问题,但它在模拟器设备(8.1)上运行良好。