NSOutlineView でいくつかのセルをアニメーション化しようとしています。左パネルのデバイスの横にある iTunes 同期アイコンとして回転するようにアイコンをアニメーション化する必要があります。
私はアニメーションの分野であまり経験がありません。インターネット全体を調べて、この簡単な解決策を見つけました。
- (void)startAnimation:(CALayer *)layer {
CABasicAnimation *spinAnimation = (CABasicAnimation *)[layer animationForKey:@"spinAnimation"];
if (!spinAnimation) {
spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
spinAnimation.toValue = [NSNumber numberWithFloat:-(5*2*M_PI)];
spinAnimation.duration = 10;
spinAnimation.repeatCount = 10000;
[spinAnimation setRemovedOnCompletion:YES];
[spinAnimation setSpeed:2.0f];
[layer addAnimation:spinAnimation forKey:@"spinAnimation"];
}
}
このソリューションで私が抱えている問題は、NSCell の異なるサブクラスのいずれにも、使用できる CALayer がないことです。そこでまず、CALayer を含む NSImageView を追加して NSCell をサブクラス化することにしました。アニメーションは機能しましたが、エクスペリエンスはひどいものでした。画像ビューは信じられないほど非効率的で、ほとんどスクロールできず、アニメーションは行の 1 つでしか機能しませんでした。
私が今やろうとしているのは、サブクラス NSButtonCell (セルにアクションを設定する必要があります。そうしないと NSImageCell になります)、drawWithFrame メソッドをオーバーライドし、受信した NSView (controlView) に subLayer を追加して、それをアニメーション化します。
現在の問題は、アニメーションが機能しないことです。
コードを残します。
PLZヘルプ!
- (void)setObjectValue:(id<NSCopying>)obj {
@try {
[super setImage:[NSImage imageNamed:@"syncImage.png"]];
[super setObjectValue:nil];
}
@catch (NSException *exception) {
NSLog(@"CustomImageViewCell-setObjectValue: %@", [exception description]);
[self setImage:nil];
[super setObjectValue:nil];
}
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
[super drawWithFrame:cellFrame inView:controlView];
if (![controlView layer]) {
CALayer *layer = [CALayer layer];
[layer setFrame:[controlView frame]];
[controlView setLayer:layer];
}
CALayer *layer = [CALayer layer];
[layer setFrame:cellFrame];
[[controlView layer] addSublayer:layer];
[self startAnimation:layer];
}
前もって感謝します。
みきわん。