Search code examples
cocoa-touchuiviewcore-animation

Animate the change of a stroke color on UIView


I'd like to "pulse" the stroke color on a path that I've drawn in a UIView's drawRect. But not sure this is possible?

- (void)highlightView {
    if (!isHighlighted) {
        [UIView beginAnimations:NULL context:nil];
        [UIView setAnimationDuration:0.75];
        self.strokeColor = kHighlightColor;
        self.strokeWidth = 2.0;
        [UIView commitAnimations];
        self.isHighlighted = YES;
    }
}

I'm only able to see the change if i setNeedsDisplay on the view. But that bypasses the animation. I can think of a few workarounds if this isn't possible like overlaying another semi-transparent view with the proper color and just fading it in... but for some reason I thought animating color properties was possible in Cocoa?!? Perhaps I'm mistaken. Hopefully one of you can set me straight.


Solution

  • You absolutely can animate colour properties with UIView, but it doesn't really make sense in the context you're dealing with right now.

    In drawRect: you are essentially painting a colour when you stroke a path, those bits just get blitted to the screen. At that point, the colour isn't really a property of the view so much as part of the painting.

    I wrote a UIView that pulsed this summer (emulating the "In Call" status bar) by creating two UIViews stack atop each other, and animating both of their color properties.