Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 2, 2008, 03:19 AM   #1
HARDWARRIOR
macrumors member
 
Join Date: Nov 2008
How to make animation from a set of images?

Hi!
I have a set of several png files. And I want to make animation from them - just change one to another after specific time period.

So I tried to implement this using layers:
First of all I made a layer for each frame and sorted them in depth:
Code:
- (void)viewDidLoad {
    [super viewDidLoad];
    
    for(NSUInteger k = 1; k <= 7; k++) {
        CALayer *frame = [CALayer layer];
        frame.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
        frame.position = CGPointMake(160.0f - 20.0f, 240.0f - 20.0f);
        frame.zPosition = -k;
        
        NSString *fileName = [NSString stringWithFormat:@"%d", k];
        UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:@"png"]];
        frame.contents = [image CGImage];
        
        [self.view.layer addSublayer:frame];
    }
}
then I implemented a method that increases zPosition on each layer by 1. First layer goes to back:

Code:
- (void)nextFrame {
    NSArray *layers = self.view.layer.sublayers;
    CALayer *firstLayer = (CALayer *)[layers objectAtIndex:0];
    [firstLayer removeFromSuperlayer];
    
    CALayer *lastLayer = (CALayer *)[layers objectAtIndex:layers.count];
    firstLayer.zPosition = lastLayer.zPosition;
    
    CALayer *layer;
    for(layer in layers)
        layer.zPosition ++;
    
    [self.view.layer insertSublayer:firstLayer below:lastLayer];     
}
calling this method when user touches the screen
Code:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {    
    [self nextFrame];    
}
It works! But now I need animation to function all by itself without touches on screen. So in viewDidAppear I perform calling of this method in infinite loop in another thread:

Code:
- (void)viewDidAppear:(BOOL)animated {
        [ NSThread detachNewThreadSelector: @selector(myThreadedMethod) toTarget: self withObject: nil ];
}

- (void)myThreadedMethod {
    NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
    
    while(YES) {
        [self nextFrame];        
        sleep(1);
    }
    
    [ pool release ];
}
But such animation does not function. Nothing changes on screen. I even tried to add [self.view.layer setNeedsDisplay] in infinite loop.

Does anybody know what the problem is? Or there is maybe less complicated way to create animation from set of separatte images?
HARDWARRIOR is offline   0 Reply With Quote
Old Dec 2, 2008, 04:50 AM   #2
priyank.ranka
macrumors member
 
Join Date: Jun 2008
Hey just simply use UIImageView cObject and set all ur frames into Animating property of that object.And call the respective funtion fr starting and stoping the animation
priyank.ranka is offline   0 Reply With Quote
Old Dec 2, 2008, 05:52 AM   #3
jnic
macrumors 6502a
 
Join Date: Oct 2008
Location: Cambridge
Quote:
Or there is maybe less complicated way to create animation from set of separatte images?
https://developer.apple.com/iphone/l...Reference.html

Code:
NSArray *images = // Your images.
UIImageView *yourAnimation = // An image view which will contain your animation.
	
yourAnimation.animationImages = images;
yourAnimation.animationDuration = 2.0;
	
[yourAnimation startAnimating];
jnic is offline   0 Reply With Quote
Old Dec 2, 2008, 07:40 AM   #4
HARDWARRIOR
Thread Starter
macrumors member
 
Join Date: Nov 2008
I need animation that I can pause and resume then from where is was paused. Not from begining. Also during pause I need to display current frame.
HARDWARRIOR is offline   0 Reply With Quote
Old Dec 2, 2008, 03:03 PM   #5
HARDWARRIOR
Thread Starter
macrumors member
 
Join Date: Nov 2008
Now I came to following implementation:

For each frame I create subview with UIImageView in it. This UIImageView has frame as it's image property.
Code:
- (void)viewDidLoad {
	[super viewDidLoad];
	
	for(NSUInteger k = 1; k <= 9; k++) {
		NSString *fileName = [NSString stringWithFormat:@"%d", k];
		UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:@"png"]];
		UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
		imageView.image = image;
		
		[self.view insertSubview:imageView atIndex:(self.view.subviews.count + 1)];
	}
	
	animationTimer = nil;
	
	NSString *path = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
	theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
	theAudio.numberOfLoops = -1;
	
	[self pause_unpause];
}
there is a music at the end)

Then every time timer
Code:
- (void)pause_unpause {
	if(animationTimer == nil) {
		animationTimer = [[NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES] retain];
		[[NSRunLoop currentRunLoop] addTimer:animationTimer forMode:NSDefaultRunLoopMode];
		[theAudio play];
	} else {
		[animationTimer invalidate];
		[animationTimer release];
		animationTimer = nil;
		[theAudio pause];
	}
	
}
ticks I call
Code:
- (void)timerFired:(NSTimer *)timer {
	[self.view bringSubviewToFront:[self.view.subviews objectAtIndex:0]];
}
that brings current frame to back
If we insert log in this method, we can value fps. On my touch 1G it's around 50 fps.

Anyway fell free to post less complex and more fast approaches to animation task!

The reason why I started all this mess with frame-by-frame animation is task where I suppsed to implement video playing on ipod. But MPMoviePlayerController does not have pause method - after every time it is stopped, it starts plying video from begining. Authors of vlc and mplayer ports to iphone did not publish their source code. So I came to an idea that there is now way no to play video on iphone except frame-by-frame. Or am I wrong?

Last edited by HARDWARRIOR; Dec 2, 2008 at 03:10 PM.
HARDWARRIOR is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Strange set of images in Application Support/iPhone Simulator MacMan988 OS X Mavericks (10.9) 2 Nov 9, 2013 12:26 AM
Displaying a set of images with varying aspect ratios macmesser Web Design and Development 0 Oct 27, 2013 03:46 PM
Wanted: App to make website images/banners GIZBUG Mac Applications and Mac App Store 0 May 25, 2013 12:48 PM
Does Core Animation make the iPhone 5 seem more sluggish? steffi iPhone 0 Sep 22, 2012 08:53 PM
How to make images/wallpapers retina ready? Trance iPad 3 Jun 9, 2012 09:20 PM

Forum Jump

All times are GMT -5. The time now is 12:30 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC