问题
I'm completely new to Core Animation, CALayer and all this stuff, so bear with me. I have a custom NSTextField using as a Label. I would want the content to animate it's position, so the whole string get's visible if it's too long for the Labels width. Now, the animation itself is working fine. I've implemented this with CABasicAnimation:
- (void)awakeFromNib {
CALayer *newLayer = [CALayer layer];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setFromValue:[NSValue valueWithPoint:NSMakePoint(0, 0)]];
[animation setToValue:[NSValue valueWithPoint:NSMakePoint(-self.attributedStringValue.size.width, 0)]];
[animation setDuration:5.0];
[animation setRepeatCount:HUGE_VAL];
[newLayer addAnimation:animation forKey:@"position"];
[self setLayer:newLayer];
[self setWantsLayer:YES];
}
The only problem is, that the drawRect: method only draws what's on the screen. So I thought I would override the drawRect: method to draw the whole attributed string. But if I do this, nothing get's drawn at all... Can anyone point me into the right direction?
Thank you!
回答1:
In general, you want to avoid overriding drawRect if at all possible, especially for CALayer objects that you are animating. That tends to lead to really dreadful performance.
What do you mean "the drawRect: method only draws what's on the screen?"
It only draws the portion of the string that's currently visible?
回答2:
I ended up using an NSTimer. Not the most beautiful solution, but at least it works.
来源:https://stackoverflow.com/questions/12035996/calayer-and-drawrect