【发布时间】:2012-10-22 12:30:59
【问题描述】:
我想在 NSImageView 中为 png 序列设置动画,但我无法使其工作。它只是不想显示任何动画。有什么建议吗?
这是我的代码:
- (void) imageAnimation {
NSMutableArray *iconImages = [[NSMutableArray alloc] init];
for (int i=0; i<=159; i++) {
NSString *imagePath = [NSString stringWithFormat:@"%@_%05d",@"clear",i];
[iconImages addObject:(id)[NSImage imageNamed:imagePath]];
//NSImage *iconImage = [NSImage imageNamed:imagePath];
//[iconImages addObject:(__bridge id)CGImageCreateWithNSImage(iconImage)];
}
CALayer *layer = [CALayer layer];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
[animation setCalculationMode:kCAAnimationDiscrete];
[animation setDuration:10.0f];
[animation setRepeatCount:HUGE_VALF];
[animation setValues:iconImages];
[layer setFrame:NSMakeRect(0, 0, 104, 104)];
layer.bounds = NSMakeRect(0, 0, 104, 104);
[layer addAnimation:animation forKey:@"contents"];
//Add to the NSImageView layer
[iconV.layer addSublayer:layer];
}
【问题讨论】:
-
发生了什么?没有显示或仅显示第一张图像? (这可能是一个愚蠢的问题)你的图像视图层支持吗? (你有没有打电话给 [iconV setWantsLayer:YES];?)
-
现在,它可以工作了!那很简单。我没有使用 [iconV setWantsLayer:YES] 我不知道。请把你的评论作为答案,我会接受的!非常感谢!
标签: cocoa core-animation calayer