问题
I have managed to draw a bar in the drawInContext method of my custom layer, PatternLayer. However, I would like to animate three things and am confused about where I can place the custom animation regulation code. Here is the interface of my custom layer:
@interface PatternLayer : CALayer
@property (nonatomic) CGFloat percentage;
@property (nonatomic) CGFloat shineProgress;
@property (nonatomic, strong) UIColor* barColor;
@property (nonatomic) CGFloat strokeWidth;
@property (nonatomic, strong) UIColor* strokeColor;
In a nutshell, the drawInContext function uses the percentage to calculate the clipping point for how much of the bar to fill, the barColor is used as the fill color, and the shineProgress is the location along the bar to place the highlight, for gradient purposes.
In one part of the animation, an Action triggers the ViewController to update the percentage and barColor.
In the second animation, the shineProgress continually cycles from 0 to 1.0 to 0 to 1.0 ... ad infinitum, with no end.
All of the examples I have been reading have been declaring CABasicAnimation or CAKeypointAnimation, but nobody ever specifies where to put that object. I have a ViewController with one PatternView, and the PatternView has one layer, the PatternLayer. From what I understand about UIActions and the such, my user interaction code goes into the View Controller, but then I'm not sure how to go about building an Animation, where to put the code that cycles the shineProgress, etc. Do I need to make the same two properties (percentage, barColor) exposed in the PatternView as well, so that the ViewController can access them? Then how do the accesses get communicated to the layer? If I try to access one of the custom properties like percentage, I can't, because self.layer in PatternView is a generic CALayer*. It really points to a PatternLayer, but I can't seem to tell it that by casting.
In essence, I read all these textbooks that discuss how to write the layer code, drawInContext, initWithLayer, needsDisplayForKey, actionForKey, etc., but I have no clue how to trigger those functions at a higher level, such as passing in the percentage & barColor values to the layer, cycling the shineProgress, etc. How do I get the eternal animation running? How do I pass parameters to the layer from the view? Any guidance on that would be much appreciated. I don't really want to add Sublayers like a lot of web examples --- just work with the one layer attached to my view.
Thank you!
Thanks for your help.
回答1:
It's not an exact answer, but I'll post it since nobody else is answering. I'm sure you can learn how to do it by simply reading the code of a similar control on Github.
Basically, drawInContext:
doesn't animate anything, it only knows how to draw the layer based on the current values of the layer's properties. CABasicAnimation
changes values of the layer's properties (according to its properties fromValue
and toValue
) and then calls drawInContext:
. It does so many times (it's based on duration
and timingFunction
), which creates the illusion of an animation.
Hope this will help you to get on the right track.
来源:https://stackoverflow.com/questions/19314355/iphone-where-to-put-code-regulating-calayer-animation