CALayer和drawRect [英] CALayer and drawRect

查看:59
本文介绍了CALayer和drawRect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Core Animation,CALayer和所有这些东西完全陌生,所以请多多包涵。
我有一个自定义NSTextField用作标签。我希望内容可以对其位置进行动画处理,因此,如果标签宽度太长,则整个字符串将可见。
现在,动画本身运行正常。我已经使用CABasicAnimation实现了此操作:

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];

}

唯一的问题是drawRect:方法只能绘制屏幕上显示的是什么。
所以我想我会重写drawRect:方法来绘制整个属性字符串。但是,如果我这样做,什么也没画出来……
谁能指出我正确的方向吗?

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?

谢谢!

推荐答案

我最终使用了NSTimer。不是最漂亮的解决方案,但至少它能起作用。

I ended up using an NSTimer. Not the most beautiful solution, but at least it works.

这篇关于CALayer和drawRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆