CALayer and drawRect

£可爱£侵袭症+ 提交于 2019-12-24 10:27:42

问题


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

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