繁体   English   中英

无法在动画块中限制子视图的动画

[英]Trouble restraining subview's animation within an animation block

为什么要限制子视图的动画? 为什么不简单地在动画之后进行其布局更改?

考虑到我的视图层次结构和这个特定用例,这是合乎逻辑的,但不可行。

我的视图层次结构:

MyViewController: UIViewController -> MyCustomView: UIView -> MyCustomScrollView: UIScrollView, UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource

为什么不可能?:

1)在各种约束更改后,我在 MyViewController 中执行此操作:

UIView.animate(withDuration: 0.3, animations: {
    self.view.layoutIfNeeded()
}) 

2) 由于 MyCustomView 是一个包含 MyCustomScrollView 的子视图(它又包含一个 UICollectionView 作为其子视图),因此布局更新会触发 CV 的willDisplay委托方法,在该方法下,我正在向 MyCustomView 添加一堆标签,准确地说。

这是我正在调用的 MyCustomView 中的函数:

func addLabel(forIndexPath indexPath: IndexPath) {

    var label: UILabel!
    label.frame = Util.labelFrame(forIndex: indexPath, fillWidth: false) // The frame for the label is generated here!

    //Will assign text and font to the label which are unnecessary to this context

    self.anotherSubView.addSubview(label) //Add the label to MyCustomView's subview
}

3) 由于这些更改从第 1 点开始就被困在动画块中,因此我会发生一些不必要的、不需要的动画。 所以,MyCustomView 的布局变化与这个动画块绑定,迫使我寻找一种方法来阻止这种情况发生

到目前为止尝试过的事情:

1) 尝试将addSubView()addLabel(forIndexPath:)包装在UIView.performWithoutAnimation {}块中。 - 没运气

2) 尝试将addSubView()addLabel(forIndexPath:)包装到另一个动画块中,时间为 0.0 秒,以查看这是否覆盖了父动画块 - 没有运气

3) 探索UIView.setAnimationsEnabled(enabled:)但似乎这不会取消/暂停现有的动画师,如果为真,将完全禁用所有动画(这不是我想要的)

总结一下,我的问题是:

我需要限制 MyCustomView 上的动画,但我需要进行所有其他所需的布局更改。 这甚至可能吗? 非常感谢提示或解决方案,TYIA!

感谢这个答案,在添加标签后从anotherSubview的层(在addLabel(forIndexPath:) )删除所有动画:

self.anotherSubview.addSubview(label)
self.anotherSubview.layer.removeAllAnimations() //Doing this removes the animations queued to animate the label's frame into the view

正是我想要的!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM