我正在尝试使用 CATextLayer 在视图中滑动文本,但它显然不起作用:
该函数animationDidStop:finished:
被调用一次,但flag
值为 NO。
代码(NSView 子类):
#import "AppStatusItemView.h"
#define DEFAULT_FRAME (NSMakeRect(0 , 0 , 80 , 20))
/* ============================================================================
MARK: -
MARK: Private Interface
=========================================================================== */
@interface AppStatusItemView (Private)
- (void)updateLayer;
- (CATextLayer *)makeTextLayer;
- (CABasicAnimation *)makeMoveAnimation;
@end
/* ============================================================================
MARK: -
MARK: Public Implementation
=========================================================================== */
@implementation AppStatusItemView
/* MARK: Init */
- (id)initWithText:(NSString *)pS {
self = [self initWithFrame:DEFAULT_FRAME];
if(self != nil) {
_text = [pS retain];
/* initialize view */
[self updateLayer];
}
return self;
}
- (void)dealloc {
[_text release];
[_textLayer release];
[super dealloc];
}
/* MARK: Properties */
@synthesize text=_text;
- (void)setText:(NSString *)pT {
if(![_text isEqualToString:pT]) {
[_text release];
_text = [pT retain];
[self updateLayer];
}
}
/* MARK: Animation Delegate */
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
if(flag) {
CABasicAnimation *myAnimation;
myAnimation = [self makeMoveAnimation];
[myAnimation setDelegate:self];
[_textLayer addAnimation:myAnimation forKey:@"slide"];
}
}
@end
/* ============================================================================
MARK: -
MARK: Private Implementation
=========================================================================== */
@implementation AppStatusItemView (Private)
- (void)updateLayer {
CABasicAnimation *myAnimation;
CATextLayer *myLayer;
myLayer = [self makeTextLayer];
myAnimation = [self makeMoveAnimation];
[_textLayer removeAllAnimations];
[_textLayer removeFromSuperlayer];
[_textLayer release];
_textLayer = [myLayer retain];
[[self layer] addSublayer:_textLayer];
[myAnimation setDelegate:self];
[_textLayer addAnimation:myAnimation forKey:@"slide"];
}
- (CATextLayer *)makeTextLayer {
CATextLayer *layer;
NSString *myString;
CGRect myFrame;
CGPoint myPosition;
myString = _text;
layer = [[CATextLayer layer] retain];
[layer setString:myString];
myFrame.origin = CGPointZero;
myFrame.size = NSSizeToCGSize([myString sizeWithAttributes:nil]);
[layer setFrame:myFrame];
myPosition.y = 1.0;
myPosition.x = [self bounds].size.width + 2.0;
[layer setPosition:myPosition];
return [layer autorelease];
}
- (CABasicAnimation *)makeMoveAnimation {
CABasicAnimation *animation;
CGPoint myPoint;
myPoint.y = 1.0;
animation = [CABasicAnimation animationWithKeyPath:@"position"];
myPoint.x = [self bounds].size.width + 1.0;
[animation setFromValue:[NSValue valueWithPoint:myPoint]];
myPoint.x = -[_textLayer bounds].size.width - 1.0;
[animation setToValue:[NSValue valueWithPoint:myPoint]];
[animation setTimingFunction:
[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation setDuration:5.0];
return animation;
}
@end
提前谢谢你,
ief2