macos - NSImageView 动画

标签 macos cocoa

我是Mac开发新手,我们有类似的方法吗 imagev = [NSArray arrayWithObjects

我需要一些类似于我们在 iOS 中所做的事情想要在 mac 中做的事情,

imageVie.animationImages = [NSArray arrayWithObjects:
 [UIImage imageNamed:@"1.png"],[UIImage imageNamed:@"2.png"],
 [UIImage imageNamed:@"3.png"],[UIImage imageNamed:@"4.png"],
 [UIImage imageNamed:@"5.png"],[UIImage imageNamed:@"6.png"],
 [UIImage imageNamed:@"7.png"] ,nil];

在 iPhone 中,我如何制作动画

问候

最佳答案

我发现有人使用 Core Animation approach to this issue这对我来说已经足够近了。我稍微修改了一下。您需要@import QuartzCore;

- (void)awakeFromNib
{
    CALayer *layer = [CALayer layer];
    NSMutableArray *spinnerImages = [NSMutableArray arrayWithCapacity:30u];
    for (NSUInteger i = 0; i < 30; ++i)
    {
        NSString *imageName = [NSString stringWithFormat:@"spinner%@", @(i)];
        [spinnerImages addObject:[NSImage imageNamed:imageName]];
    }
    self.spinnerImages = spinnerImages;
    layer.frame = self.imageView.bounds;
    [self.imageView setLayer:layer]; // This view is just a container for the layer. Its frame can be managed by a xib.
    self.imageView.wantsLayer = YES;

    self.spinnerLayer = layer;
}

然后你可以像这样设置动画:

- (void)stopAnimating
{
    if ([self.layer.animationKeys containsObject:kAnimationKey])
    {
        [self.layer removeAnimationForKey:kAnimationKey];
    }
}

- (void)startAnimating
{
    if ([self.layer.animationKeys containsObject:kAnimationKey])
    {
        return;
    }
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:kAnimationKey];
    [animation setCalculationMode:kCAAnimationDiscrete];
    [animation setDuration:1.0f];
    [animation setRepeatCount:HUGE_VALF];
    [animation setValues:self.spinnerImages];
    [self.spinnerLayer addAnimation:animation forKey:kAnimationKey];
}

关于macos - NSImageView 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064328/

相关文章:

macos - 如何在 OS X 10.8 中使用 Applescript 将窗口移动到目标空间?

macos - GCD block 在调用时触发 EXC_BAD_ACCESS

macos - 以编程方式对 NSTableView 进行排序

swift - Xcode 和 Swift - 没有标题栏但带有关闭、最小化和调整大小按钮的窗口

python - 实现 PyObjc NsObject 子类

macos - 你如何编写 Mac 包来安装而不需要 Lion 的管理员权限?

ruby-on-rails - 无法加载此类文件 -- phantomjs/poltergeist (LoadError)

objective-c - 显示 NSArrays 的 NSDictionary 最好的是什么?

objective-c - cocoa 和缓存

objective-c - 更改 NSTableView 备用行颜色