开发者

Changing the Color of a Path Using Animation - iPhone Dev

I am working on a "connect the dots" app and would like the line that is drawn from one dot to another to flash briefly to indicate to the user that they have successfully completed the correct line. I initially thought the best way to do this would be to alternate the stroke color between black and white. I have no animation experience and everything I've read regarding the Core Animation Types indicate that only properties of a view can be animated. I've considered creating a custom view that is as long and thin as the line it would represent, and then changing the background color. However, this seems extremely inefficient considering the number of lines needed to complete an object in a standard "connect the dots" image.

I have overridden the touchesBegan开发者_StackOverflow社区/touchesMoved/touchesEnd methods while incorporating CGContext methods to create and stroke paths to specific locations on the screen.

Is there a major concept I'm missing that would allow me to animate the color of the path? Or is my approach using UITouch/CGContext a problem when considering animation? Thank you for your time in advance.


There are a lot more options available for animation if you use CALayer's animation abilities. However that comes with complexity.

I'm not sure you need to animate anything at all. Animation implies intermediate values. You can simply delay 0.25s or whatever, then redraw the path in the alternate color.

If you really want animation, draw the path in two views, and animate the alpha of one back and forth between 0.0 and 1.0. That would give a smooth fade-y effect to the flashing. You can also use that technique to flash without animation if redrawing the path each time is not efficient (but unless you are redrawing 1000 paths, I think you will have enough cycles to redraw the path every 0.25s).


Having little experience in drawing and animation, it seemed logical to look up animation methods as a way of making lines flash. The answer to my question above (thank you morningstar) helped set me in the right direction.

I created two drawing methods. One that would draw a black line and another that would draw a lighter line. I then used the performSelector method and called each drawing method alternating every .05 seconds for 1 second total. This redraws the line in black then white quickly and repeatedly, giving it the flashing effect I was looking for. Having limited experience in this language, discovering the performSelector method is extremely valuable and something I will most likely be using quite a bit in the future!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜