WARNING : This content was auto generated from a backup file, links and images may be broken.

The UIImageView provided by UIKit is a very useful class it provides a very simple way to add Images into a View hierarchy. It also provides a basic way to provide texture animation though it does have it is limitations. 1) you have to load all the images up front which is fine for a bunch of small images but you do not really want to do that for fullscreen animations with more than 10 frames. You will start to run low on memory and if you are unable to free enough memory to OS will kill your application. 2) there is no callback support provided when using the UIImageView animations all you can do is start / stop and check to see if it is animating. If you want to do anything else you need to create your own timer and hope that you get the timing right. The following code is to try and build upon the UIImageView and provide a simple way to add full screen texture animations to your project and have the ability to receive callbacks when an animation starts / stops and when a frame changed.

The UIImageView provides the following API for texture animation:
Starts animating the images in the receiver.

- (void)startAnimating

Stops animating the images in the receiver.

- (void)stopAnimating

Returns a Boolean value indicating whether the animation is running.

- (BOOL)isAnimating

The amount of time it takes to go through one cycle of the images.

@property(nonatomic) NSTimeInterval animationDuration

An array of UIImage objects to use for an animation.

@property(nonatomic copy) NSArray *animationImages

Specifies the number of times to repeat the animation.

@property(nonatomic) NSInteger animationRepeatCount

So how do you use it Apples developers have made it really simple load an array UIImages. Add the image array into the UIImageView via the animationImages property set the animations total duration. Insert the View into the hierarchy if it is not already in there and start the animation using the function startAnimating. Simple and easy I wanted to provide some thing similar and I copied the provided APU and add the extra functionality I wanted such as callbacks.

UIImageViewAnimator header file:
//  UIImageViewAnimator.h
//  Created on 16/01/2010.

#import <UIKit/UIKit.h>

@interface UIImageViewAnimator : UIView
// Image View to Animate.
IBOutlet UIImageView *imageview;

// Timer
NSTimer *animtimer;
// Array of Image Names
NSArray *imageNames;
// Current Animation Index
// Animation Duration



@property (nonatomic)NSIntegerindex;
@property (nonatomicreadonly)NSIntegercount;
@property (nonatomiccopy)NSArray *imageNames;
@property (nonatomic)NSTimeIntervalduration;
@property (nonatomic)BooleancacheImages;
@property (nonatomic)Booleanreverse;

@property (nonatomicretain)iddelegate;
@property (nonatomic)SELstartSelector;
@property (nonatomic)SELstopSelector;
@property (nonatomic)SELframeChangeSelector;

- (void) startAnimating;
- (void) startAnimatingWithContext:(id)_context;
- (void) stopAnimating;
- (Boolean) isAnimating;

- (void) precache;

The big difference between the two implementations is that you provide an array of Image Names rather than an array of UIImage’s this will let the underlying animation code dynamically load the images as they are needed reducing the amount of memory used and decreasing the lightly hood of running out of memory. There is also an extra startAnimating that takes a context or user data object this will be stored and passed back to the callbacks the callbacks are called (if provided they are optional) when the animation is started / stopped and when a frame has been incremented. Caching: The code provides two ways of loading images the caching system uses the [UIImage NamesImage:@"name"] function this provides a texture cache provided by UIKit if we start to run low on resources the OS will automatically flush some of the images that are not being used. The non-caching system uses the [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"name" ofType:nil] function this will by pass the OS image cache and when the image is released it is memory will also be freed. I think that at some point I want to implement a better of doing this. The code implementation is in no way perfect and really could do with a bit more work but it has been implemented to meet my needs. Feel free to extend and re-work it as much as you like I have a few changes that I would really like to make but time as always is against me. Download : UIImageViewAnimator.h UIImageViewAnimator.m Sample Project : UIImageViewAnimatorSample Example Use :
// This sample code and may not compile as it is.

// Fill the animator with the image list
- (void) createAnimation
      NSMutableArray * dataset = [NSMutableArray arrayWithCapacity:20];
      for ( int i=0; i<20; i )
            NSString * imageName = [NSString stringWithFormat:@"animationImageName%d.png"i];
            [dataset addObject:imageName];

      // image animator constructed inside a nib
      [imageAnimator setImageNames:dataset];

      // set the duration to show the 20 images in 2 seconds
      // thats 1 every tenth of a seconds though it may be
      // slower due to having to load each individual image.
      [imageAnimator setDuration:2];

      // This will use a different loading function
      // where the OS will try and keep as much
      // of the image data in memory as possible.
      // It may improve the speed of the second
      // playback.
      [imageAnimator setCacheImages:TRUE];

      // Setup the delegates and callbacks
      [imageAnimator setDelegate:self];
      [imageAnimator setStartSelector:@selector(start:)];
      [imageAnimator setStopSelector:@selector(stop:)];

- (void) start:(id)_context
       NSLog( @"Animation playback has been triggered" );

- (void) stop:(id)_context
       NSLog( @"Animation playback has finished" );

- (void) playAnimation
        // If the animation has a few images
        // you can try to pre load all the images
        // this will proved a faster/smoother
        // animation as they will be resident
       // in main memory.
       //[imageAnimator preCache];

       // Reverse will permit you to
       // play the animation backwards
       // Note : You will need to set the
       // index to be the last frame to
       // make use of this feature.
       //[imageAnimator setIndex:[imageAnimator count]-1];
       //[imageAnimator setReverse:TRUE];

       // start the animation playing
       [imageAnimator startAnimating];

Note : The current implementation requires the animator to be constructed and setup via Interface Builder. The simplest way to do is change the root views class to be that of the UIImageViewAnimator add a UIImageView into the high-archy and link the image view to the animator.


Comments powered by Disqus