Animate the change of a stroke color on UIView

时光怂恿深爱的人放手 提交于 2019-12-11 12:23:18

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1614630/animate-the-change-of-a-stroke-color-on-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!