mac 开发 使用NSImageView实现多张图片动画

茫茫网络却又寥寥无几...


Paste_Image.png
//
//  ViewController.m
//  mac_animation_Test
//
//  Created by Eric luo's Macbook Pro on 2017/6/7.
//  Copyright © 2017年 Eric luo‘s Macbook Pro. All rights reserved.
//

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

static NSString *kAnimationKey = @"contents";

@interface ViewController ()
@property (nonatomic, strong) NSImageView *imageView;
@end

@implementation ViewController

- (NSImageView *)imageView {
    if (!_imageView) {
        _imageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, 130, 130)];
    }
    return _imageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.imageView];
    
    NSMutableArray *icons = [NSMutableArray arrayWithCapacity:5];
    for (int i = 0; i < 5; i++) {
        NSString *imageName = [NSString stringWithFormat:@"ship-anim%d", i];
        NSImage *image = [NSImage imageNamed:imageName];
        [icons addObject:image];
    }
    
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:kAnimationKey];
    [animation setCalculationMode:kCAAnimationDiscrete];
    [animation setDuration:1.0f];
    [animation setRepeatCount:HUGE_VALF];
    [animation setValues:icons];
    
    CALayer *layer = [CALayer layer];
    layer.frame = self.imageView.bounds;
    layer.bounds = self.imageView.bounds;
    [layer addAnimation:animation forKey:kAnimationKey];
    
    [self.imageView setLayer:layer];
    [self.imageView setWantsLayer:YES];
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}


@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 社交红利阅读笔记 书名:社交红利(修订升级版) 作者:徐志斌 出版社:中信出版社 正文前笔记: 推荐序1摘要 社交...
    凫水阅读 9,072评论 4 26
  • 01 零用钱:理财教育的出发点 开学季来到,孩子的开销也多起来,这时候对孩子进行零用钱教育正当时。首先要让孩子正...
    紫竹姑娘阅读 246评论 0 2
  • 目中有人,才有路; 心中有爱,才有度。 一个人的宽容,来自一颗善待他人的心。 一个人的涵养,来自一颗尊重他人的心。...
    王志2017阅读 848评论 0 0
  • 麦凯:目前的工作环境以及气氛比过去好太多了,公司也经常变着花样让我们开心,但是我们好像还是不太喜欢工作?…… 近日...
    寻寻觅觅终相见阅读 221评论 0 0