我正试图激活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。因此,首先,我决定将NSCell子类添加一个包含CALayer的NSImageView。动画效果很好,但体验很糟糕,图像视图效率极低,我几乎无法滚动,动画只能在其中一行中工作。
我现在要做的是子类NSButtonCell (我需要为单元格设置一个操作,否则它将是一个NSImageCell),重写drawWithFrame方法并向接收到的NSView (controlView)添加一个subLayer,然后将其动画化。
现在的问题是动画不能工作。
我把密码留给你。
请帮帮忙!
- (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];
}提前谢谢。
米基万。
发布于 2011-07-26 21:45:06
编辑:这个答案只适用于iOS --最初的问题是用于OSX的。
我建议使用UIImageView的animationImages和animationDuration属性。(我认为这基本上就是苹果对UIActivityIndicator类所做的。)这应该比不断重新计算旋转图像更便宜/更好。
因此,使用图像编辑器,并以顺序旋转的位置保存图标的多个“框架”,并使用这些“框架”填充animationImages属性的数组。
https://stackoverflow.com/questions/6793098
复制相似问题