1
幻燈片文本
我試圖與CATextLayer一個以幻燈片文本,但它顯然不是工作:試圖與CAAnimation和CATextLayer
功能animationDidStop:finished:
被調用一次,但沒有爲flag
值。
代碼(子類的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