Either I haven’t done iOS animation for too long and my memory degenerated, or things changed. I remember if the value is changed while an animation is going on then the animation will be interrupted and the property immediately jump to the new value. For example, I expect to see the button moves with animation for 0.5 second then jumps to the new location with a bigger hight w/o animation.
button.frame = CGRect(x: 80, y: 80, width: 80, height: 50)
UIView.animate(withDuration: 1) {
// Change location
button.frame = CGRect(x: 200, y: 200, width: 80, height: 50)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
// Change location and height
button.frame = CGRect(x: 150, y: 150, width: 80, height: 100)
}
However, what I’m seeing is the value change in the middle of animation does nothing in effect. The animation goes on to completion without any interruption and the end value of frame is CGRect(x: 200, y: 200, width: 80, height: 50) not CGRect(x: 150, y: 150, width: 80, height: 100).
After Google failed me I tried ChatGPT and Bard, the results just reenforced my confusion.
Me:
I have a question about iOS core animation. What happens if I set a view’s frame while a frame animation of the same view is still going on? Which will be the frame value after the animation completes, the value I just set or the original end state value of the animation?
ChatGPT agrees with my memory:
In iOS Core Animation, if you set a view’s frame while a frame
animation of the same view is still in progress, the new frame value
that you set will override the animated values, and the view will
immediately jump to the new frame.Core Animation uses layers to represent views, and animations are
applied to these layers. When you set the frame of a view, you are
essentially updating the underlying layer’s properties. If you change
the frame property directly, it interrupts any ongoing animations and
sets the layer’s frame to the new value immediately.In summary, the frame you set during an ongoing animation takes
precedence, and the animation is interrupted. The final state of the
view will be the frame you just set.
Bard is kind in the middle:
When you set a view’s frame while a frame animation is in progress,
the following occurs:
- Immediate Visual Update: The view’s frame updates instantly to the new
value you set, visually overriding the ongoing animation. This creates
a jump in appearance.- Animation Continuation: The underlying animation
continues based on its original settings, including the original end
state value.- Final Frame Value: Once the animation completes, the
view’s frame settles at the original end state value, even if you set
a different value during the animation.Key Points:
- Visual Override: Setting the frame mid-animation only affects the
visual appearance, not the animation’s trajectory.- Original End State Preserved: The animation always adheres to its initial end state,
regardless of mid-animation frame changes.




