iOS - Smooth Color Change Transition/Animation

JimmyJammed picture JimmyJammed · Nov 5, 2013 · Viewed 25.7k times · Source

I want to have a smooth color transition that goes across the entire spectrum (i.e. red, blue, green, yellow, orange, etc.)

Also want to be able to have smooth transitions of colors in specific spectrum (i.e. all reds).

Are there any simple algorithms/recursive functions/formulas that can help simplify this process?

Answer

daltonclaybrook picture daltonclaybrook · Nov 5, 2013

One very simple way to accomplish this would be to use a UIView animation block.

[UIView animateWithDuration:1.0 animations:^{
    view.backgroundColor = [UIColor redColor];
}];

This will interpolate between whatever view's previous background color was, and red over the course of 1 second.

Swift:

UIView.animate(withDuration: 1.0) { 
    view.backgroundColor = .red
}