Full Screen Animator for a UIImageView
The UIImageView provides the following API for texture animation:
startAnimatingStarts animating the images in the receiver.
- (void)startAnimating
stopAnimatingStops animating the images in the receiver.
- (void)stopAnimating
isAnimatingReturns a Boolean value indicating whether the animation is running.
- (BOOL)isAnimating
animationDurationThe amount of time it takes to go through one cycle of the images.
@property(nonatomic) NSTimeInterval animationDuration
animationImagesAn array of UIImage objects to use for an animation.
@property(nonatomic, copy) NSArray *animationImages
animationRepeatCountSpecifies 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’s 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
{
@private
// Image View to Animate.
IBOutlet UIImageView * imageview;
// Timer
NSTimer * animtimer;
// Array of Image Names
NSArray * imageNames;
// Current Animation Index
NSInteger index;
// Animation Duration
NSTimeInterval duration;
id context;
id delegate;
SEL frameChangeSelector;
SEL startSelector;
SEL stopSelector;
Boolean cacheImages;
Boolean reverse;
Boolean imageset;
}
@property (nonatomic) NSInteger index;
@property (nonatomic,readonly) NSInteger count;
@property (nonatomic,copy) NSArray * imageNames;
@property (nonatomic) NSTimeInterval duration;
@property (nonatomic) Boolean cacheImages;
@property (nonatomic) Boolean reverse;
@property (nonatomic,retain) id delegate;
@property (nonatomic) SEL startSelector;
@property (nonatomic) SEL stopSelector;
@property (nonatomic) SEL frameChangeSelector;
- (void) startAnimating;
- (void) startAnimatingWithContext:(id)_context;
- (void) stopAnimating;
- (Boolean) isAnimating;
- (void) precache;
@end
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’s 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.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콜백 함수를 Angular 하위 구성 요소에 전달이 예제는 구성 요소에 함수를 전달하는 것과 관련하여 최근에 직면한 문제를 다룰 것입니다. 국가 목록을 제공하는 콤보 상자 또는 테이블 구성 요소. 지금까지 모든 것이 구성 요소 자체에 캡슐화되었으며 백엔드에 대한 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.