【问题标题】:Remove UIView inside the completion block of animateWithDuration:completion在 animateWithDuration:completion 的完成块中删除 UIView
【发布时间】:2015-05-05 15:18:49
【问题描述】:

我正在使用 animateWithDuration:completion 为 UIView 的某些属性设置动画。在这些动画完成后,我想从他的超级视图中删除这个视图。 问题是,如果我在完成块 removeFromSuperView 内添加,我根本不会得到动画。我找到的解决方法是删除 dispatch_after 中的视图。为什么?

     var loadingView: Loading!  //This is a UIView loaded from a Nib
    let delaySecs: NSTimeInterval = 2
    UIView.animateWithDuration(delaySecs, animations: { () -> Void in
        self.loadingView.cherrysImageView.transform = CGAffineTransformMakeScale(2, 2)
        self.loadingView.cherrysImageView.alpha = 0
    }) { (succeed) -> Void in
        //Can't remove the view here, otherwise i get NO animation
        //self.loadingView.removeFromSuperview()
    }

        //And if i add this instead of removing the view in the completion block, it works as expected
    let delay = delaySecs * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue()) { () -> Void in
        self.loadingView.removeFromSuperview()
    }

【问题讨论】:

  • 我复制了你的代码,它可以很好地删除完成块中的超级视图。

标签: ios swift


【解决方案1】:

动画完成块在动画过程中被多次调用,但只有在成功为真时才能删除所有内容。

var loadingView: Loading!  //This is a UIView loaded from a Nib
let delaySecs: NSTimeInterval = 2
UIView.animateWithDuration(delaySecs, animations: { () -> Void in
    self.loadingView.cherrysImageView.transform = CGAffineTransformMakeScale(2, 2)
    self.loadingView.cherrysImageView.alpha = 0
}) { (succeed) -> Void in
    if succeed {
        self.loadingView.removeFromSuperview()
    }
}

【讨论】:

  • 我认为这不是正确的答案。我在完成块中放了一个日志,它没有被多次调用。删除完成块中视图的 OP 代码对我有用。
  • @rdelmar 如果您同时有不同的动画,它会被多次调用。 UIView 动画有问题。此外,代替“成功”,更合适的名称应该是“完成”,因为当动画真正完成时它是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-14
  • 2014-09-16
  • 1970-01-01
相关资源
最近更新 更多