简体   繁体   中英

Trouble restraining subview's animation within an animation block

Why restrain a subview from animation? Why not simply carry out its layout changes after the animation?

That's logical but not feasible considering my view hierarchy and this particular use-case.

My view hierarchy:

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

Why is it not possible?:

1) I do this in MyViewController after various constraint changes:

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

2) Since MyCustomView is a subview which contains MyCustomScrollView ( which in turn contains a UICollectionView as its subview ), the layout update triggers CV's willDisplay delegate method under which I'm adding a bunch of labels to MyCustomView, to be precise.

Here's the function in MyCustomView that I'm calling:

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) Since these changes get caught up within the animation block from point 1, I get some unnecessary, undesired animations happening. And so, MyCustomView's layout change is bound with this animation block, forcing me to look for a way to restrain this from happening

Things tried so far:

1) Tried the wrap the addSubView() from addLabel(forIndexPath:) inside a UIView.performWithoutAnimation {} block. - No luck

2) Tried the wrap the addSubView() from addLabel(forIndexPath:) inside another animation block with 0.0 seconds time as to see if this overrides the parent animation block - No luck

3) Explored UIView.setAnimationsEnabled(enabled:) but it seems like this won't cancel/pause the existing animators, and will completely disable all the animations if true (which is not what I want)

To sum this all up, my problem is:

I need to restrain the animations on MyCustomView but I need all my other desired layout changes to take place. Is this even possible? Would really appreciate a hint or a solution, TYIA!

Thanks to this answer, removing all the animations from anotherSubview 's layer(inside the addLabel(forIndexPath:) ) after adding the label:

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

does exactly what I want!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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