NSCell 上的动画
我正在尝试为 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];
}
提前致谢。
米基万。
I'm trying to animate some of the cells in my NSOutlineView. I need to animate an icon to rotate as the iTunes syncing icon next to the device, on the left panel.
I'm not really experienced in the animating field, I found this simple solution by looking all over the internet:
- (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"];
}
}
The problem I'm having with this solution is that neither of the different subclasses of NSCell has a CALayer that I can work with. So at first I've decided to subclass NSCell adding an NSImageView that contains an CALayer. The animation worked, but the experience was awful, the image view was incredibly inefficient, I could barely scroll and the animation only worked in one of the rows.
What I'm trying to do now is subclass NSButtonCell (I'll need to set an action to the cell, otherwise it would've been an NSImageCell), override the drawWithFrame method and add a subLayer to the NSView (controlView) received and then animate it.
The problem right now is that the animation won't work.
I leave you the code.
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];
}
Thanks in advance.
Mikywan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:此答案仅适用于 iOS - 原始问题适用于 OSX
我建议使用
UIImageView
的animationImages
和 <代码>animationDuration 属性。 (我相信这本质上就是苹果为其 UIActivityIndicator 类所做的事情。)这应该比不断重新计算旋转图像更便宜/性能更好。因此,使用图像编辑器并按顺序旋转位置保存图标的多个“帧”,并使用它们填充
animationImages
属性的数组。Edit: This answer only works for use in iOS -- Original Question is for use in OSX
I would suggest using
UIImageView
'sanimationImages
andanimationDuration
properties. (I believe that this is essentially what Apple does for their UIActivityIndicator class.) This should be cheaper/perform better than constantly recalculating the rotated image.So, use an image editor and save out multiple "frames" of your icon in sequential rotated positions, and use those to populate an array for the
animationImages
property.