NSImageView animations

| | August 5, 2015

I am new to Mac development, Do we have any methods like
imagev = [NSArray arrayWithObjects

I need some thing like what we do in iOS want to do in 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];

In iPhone, How can i animate

Regards

2 Responses to “NSImageView animations”

  1. I found someone using a Core Animation approach to this issue which was close enough for me. I modified it slightly. You need to @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;
    }
    

    Then you can animate it like this:

    - (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];
    }
    
  2. Cocoa doesn’t have anything like animatedImageWithImages:duration:. Images in Cocoa can vary in space (different resolutions) and color depth, but not time; a single image is always a static image, never animated.

    (There might be an exception for animated GIFs, but GIFs can’t display more than 255 or 256 colors per frame, and do not support partial transparency. Moreover, I haven’t tried creating or displaying GIFs using the NSImage or CGImage machinery.)

    What you’ll need to do is create not an image, but a movie. Add images to the movie, varying each one’s duration to achieve the playback speed you want. Then, display your movie in a movie view, optionally with the controller hidden.

Leave a Reply