cocoa - Cocoa Objective-C 中的简单动画

标签 cocoa animation

我为 iOS 游戏编写了一个简单的动画,并尝试将其转换为 Mac OS 游戏。

这是我的 iOS 代码

NSMutableArray *imgArr = [[[NSMutableArray alloc] init] autorelease];

    for(int i = 1; i < 6 + 1; i++) {
        [imgArr addObject: [UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png", filePrefix, i]]];
    }

    UIImageView * animation = [[UIImageView alloc] initWithFrame:rect];
    animation.animationImages = imgArr;

    animation.animationRepeatCount = 1;
    [animation startAnimating];

    [self.view addSubview:animation];

至于 cocoa ,这部分出现错误..我该如何解决这个问题?

//animation.animationImages = imgArr;

//animation.animationRepeatCount = 1;
//[animation startAnimating];

带有计时器的新代码

for(int i = 0; i < 6; i++)
    [NSTimer scheduledTimerWithTimeInterval:1
                                     target:self
                                   selector:@selector(anim)
                                   userInfo:nil
                                    repeats:NO];


- (void)anim {
    ++num;


    NSImageView *animation = [[NSImageView alloc] initWithFrame:NSMakeRect(shipPosition.x, shipPosition.y, shipSize.width, shipSize.height)];

    [animation setImage: [NSImage imageNamed:[NSString stringWithFormat:@"explosprite%d.png", num]] ];
    [objectView addSubview:animation];

    [animation release];
}

最佳答案

Cocoa 中根本不存在 UIImageView。 Cocoa 并不真正支持 Sprite 动画,就像您在这里尝试做的那样。 AFAIK 最常见的方法就是设置一个 NSTimer 并在计时器触发时适当更新图像。

您还可以创建一个处理动画图像的自定义 CALayer 子类。我不确定这一定更容易,但这是一个选择。

关于cocoa - Cocoa Objective-C 中的简单动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14062809/

相关文章:

html - CSS3动画——两个问题

reactjs - react : How to Animate Expanding and Collapsing Div When the Size of the Content is Not Knowable

objective-c - 检查应用程序的主窗口是否打开并加载

macos - NSWindow Animate 显示/隐藏标题栏

cocoa - 在 Cocoa 中,如何设置数据包的 TTL?

Cocoa webView - 禁用所有交互

javascript - 禁用过渡动画

cocoa - Swift 的 GeneratorOf<T> 疯狂初始化

javascript - 给定 (t, p0, p1, p2, p3) 时,三次贝塞尔曲线未返回正确的 Y 值

javascript - 如何使页面中的链接在鼠标滑过时振动?