2
votes

Is there a way to animate the following:

UIView *exampleView = //something
[exampleView.superview bringToFront:exampleView];

In a for loop so that every view in a view hierarchy would be brought to the front of the hierarchy one at a time with a delay between each view change, because when I put the above line of code into an animation inside a for loop all of the views are brought to the front of the hierarchy at once since animations occur immediately in code and only change over time in the way they look on screen.

2
How would this animation even look? I'd recommend setting alpha to 0, bringing to front, then animating alpha to 1. - nhgrif

2 Answers

3
votes

You can only animate the changing of animatable properties. You cannot animate the enactment of arbitrary methods. And only certain properties are animatable (they are explicitly listed as such). A property is animatable because there is some way to represent visually the intermediate stages between the initial and final values.

You might investigate the layer's zPosition property, which is expressly said to be animatable, but I do not know what the visual effect looks like.

0
votes

You can also animate transitions using UIView's transition(from:to:duration:options:completion:) or transition(with:duration:options:animations:completion:) functions. Links to the Apple documentation to these functions are here and here.

Please keep in mind that Apple is discouraging use of these animation techniques as of iOS 13 and recommending to use UIViewPropertyAnimator class functions instead.

Let me give an example of the usage in Swift:

let containerView = UIView()
let transitionFromView = UIView()
containerView.addSubview(transitionFromView)
//add your layout constraints to the transitionFromView within the containerView

let transitionToView = UIView()
containerView.addSubview(transitionToView)
//add your layout constraints to the transitionToView within the containerView
...
func performTransition1() {
   //since both transitionFromView and transitionToView have already been added 
   //to their parent's subviews, we can use showHideTransitionViews animation option
   //in this example I am using transitionCrossDissolve animation; however, 
   //there are other options that can be used instead such as 
   //transitionFlipFromRight
   UIView.transition(from: transitionFromView,
                     to: transitionToView,
                     duration: 0.5,
                     options: [.showHideTransitionViews, .transitionCrossDissolve],
                     completion: nil)
}

ps: full list of animation options can be found here