美文网首页
mac 开发 使用NSImageView实现多张图片动画

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

作者: 景彧 | 来源:发表于2017-06-07 16:11 被阅读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
    
    

    相关文章

      网友评论

          本文标题:mac 开发 使用NSImageView实现多张图片动画

          本文链接:https://www.haomeiwen.com/subject/wrcafxtx.html