我正在嘗試爲我的NSOutlineView中的某些單元格設置動畫。我需要爲圖標設置動畫效果,將其作爲左側面板上設備旁邊的iTunes同步圖標進行旋轉。NSCell上的動畫
我真的不經歷在動畫領域,我發現這個簡單的解決方案通過查看所有在互聯網上:
- (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。所以起初我決定爲NSCell子類添加一個包含CALayer的NSImageView。雖然動畫效果很好,但是體驗非常糟糕,圖像視圖的效率非常低,我幾乎無法滾動,動畫只能在其中一行中運行。我想現在要做的是子類NSButtonCell(我需要設置一個動作到單元格,否則它將會是一個NSImageCell),重寫drawWithFrame方法並添加一個subLayer到NSView( controlView)接收並動畫。
現在的問題是,動畫將無法正常工作。
我離開你的代碼。
PLZ HELP !.
- (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];
}
在此先感謝。
Mikywan。
我正在與蘋果的支持解決方案。只要我有,我會在這裏發佈。 – mikywan
嗨,你有解決方案嗎? – SAKrisT
對不起,蘋果公司的人給我的也沒有用,現在我沒有時間繼續。 – mikywan