mac 开发 使用NSImageView实现多张图片动画
2017-06-07 本文已影响110人
景彧
茫茫网络却又寥寥无几...
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