'CABasicAnimation doesn't work on subview?
I'm new to CABasicAnimation. I want to create a auto-scrolling text panel that when the text is longer than the panel, the text will horizontal scrolling. But it seems that the CABasicAnimation doesn't work on subview. How to scroll the text view using CABasicAnimation?
@interface CTextPan : NSView
@end
@interface CTextPan()
@property (nonatomic, strong) NSTextField* text;
@end
@implementation CTextPan
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
self.wantsLayer = YES;
[self initView];
}
return self;
}
- (void)initView {
_text = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 20)];
_text.backgroundColor = [NSColor clearColor];
_text.bordered = NO;
_text.editable = NO;
_text.stringValue = @"my text";
[self addSubview:_text];
CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"position"];
translation.fromValue = [NSValue valueWithPoint:_text.frame.origin];
translation.toValue = [NSValue valueWithPoint:NSMakePoint(_text.frame.origin.x+100, 0)];
translation.duration = 2;
translation.repeatCount = 1;
translation.autoreverses = YES;
[_text.layer addAnimation:translation forKey:@"translation"]; //the _text doesn't scroll!
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
