Trouble loading 2 separate image sequences in the UIImageView - iphone

I don't know if this is possible, but I need to load one image sequence in the imageView and have it play only once, then unload that sequence and load the second image sequence into the same imageView control.
Here is the code I'm using for loading and playing the first sequence.
imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"openingSeq0001.jpg"],
...etc.
[UIImage imageNamed:#"openingSeq0140.jpg"],nil];
imageView.animationDuration = 2;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
[self.view addSubview:imageView];
[super viewDidLoad];
I now want to load and play the second sequence into the same imageView control once it has played through once. I have tried if statements, even a do while loop to make sure that the animation was at least played once and decremented the counter 1 and passed the value to the imageView.animationRepeatCount to equal 0.
imageView.animationRepeatCount = 0;
I thought I could figure it out, but I'm missing something somewhere. I can get the second grouping of images to run or the first and then is just stops. I used a do while loop for that.
I used this to make it work:
SEL methodSelector = #selector(startSeqTwo);
[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:methodSelector userInfo:nil repeats:NO];
I used the selector after the first block of code to load the first sequence and called method:
-(void)startSeqTwo;

I have not done it but you could try using self performSelectorOnMainThread:(SEL) withObject:(id) waitUntilDone:(BOOL) function.
-(void)animateSequence{
[self performSelectorOnMainThread:#selector(animateFirstSequence) withObject:nil waitUnitilDone: YES];
[self performSelectorOnMainThread:#selector(animateSecondSequence) withObject:nil waitUnitilDone: YES];
}
-(void)animateFirstSequence{
imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"openingSeq0001.jpg"],
...etc.
[UIImage imageNamed:#"openingSeq0140.jpg"],nil];
imageView.animationDuration = 2;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
}
-(void)animateSecondSequence{
imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"openingSeq0001.jpg"],
...etc.
[UIImage imageNamed:#"openingSeq0140.jpg"],nil];
imageView.animationDuration = 2;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
}
Edit
You can also try checking if the UIImageView is stil animating and wait until finished to play another sequence. You would do it like this:
-(void)animateSequence{
//First sequence
imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"openingSeq0001.jpg"],
...etc.
[UIImage imageNamed:#"openingSeq0140.jpg"],nil];
imageView.animationDuration = 2;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
while([imageView isAnimating])
//Do nothing
//second sequence
imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"openingSeq0001.jpg"],
...etc.
[UIImage imageNamed:#"openingSeq0140.jpg"],nil];
imageView.animationDuration = 2;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
}

SEL methodSelector = #selector(startSeqTwo);
[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:methodSelector userInfo:nil repeats:NO];
This worked.
I used the selector after the first block of code to load the first sequence and called method: -(void)startSeqTwo;

Related

UIImage to slide downwards using animation

How can implement image sliding down animation in ios app?
I've used this code:
imageview.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"img10.png"],
[UIImage imageNamed:#"img11.png"],[UIImage imageNamed:#"img12.png"],[UIImage imageNamed:#"img13.png"],nil];
imageview.animationDuration = 10.00;
imageview.animationRepeatCount = 0;
[imageview startAnimating];
But this is only a simple slide show.
Help me.
try with this code..
imageview = [[UIImageView alloc] initWithFrame:self.view.frame];
imageview.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"img10.png"],
[UIImage imageNamed:#"img11.png"],[UIImage imageNamed:#"img12.png"],[UIImage imageNamed:#"img13.png"],nil];
imageview.animationDuration = 1.25;
imageview.animationRepeatCount = 10;
[imageview startAnimating];
[self.view addSubview:animationView];
[imageview release];
Please try following code
-(void) createNewImage {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,460)];
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"img10.png"],
[UIImage imageNamed:#"img11.png"],
[UIImage imageNamed:#"img12.png"],
[UIImage imageNamed:#"img13.png"],nil];
[imageView setAnimationRepeatCount:10];
imageView.animationDuration =1.5;
[imageView startAnimating];
[NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:#selector(setLastImage:) userInfo:nil repeats:NO];
}
-(void)setLastImage:(id)obj
{
[imageView performSelectorOnMainThread:#selector(setImage:) withObject:[UIImage imageNamed:#"img12.png"] waitUntilDone:YES];
}
You can change the centre of UIImageView using imgVw.centre, you can update the centre with NSTimer. Vary the centre according to the screen, you can get screen height from
[[UIScreen mainScreen]bounds].size.height
theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(updateTimer:)
userInfo:nil
repeats:YES];
- (void)updateTimer:(NSTimer *)theTimer {
//update centre;
imgVw.centre.y += 4;
// you can update the image also if needed use if case
imgVw.image = image2;
}

delay imageviews xcode

I hit the play button 6 image views will be filled with the pressed images, I want it to view 1, wait 1 second,view2, wait one second etc.
which code should I be adding to:
[imageview7 setImage:imageview1HG.image];
[imageview8 setImage:imageview2HG.image];
[imageview9 setImage:imageview3HG.image];
[imageview10 setImage:imageview4HG.image];
[imageview11 setImage:imageview5HG.image];
[imageview12 setImage:imageview6HG.image];
I tried this: but it doesn't work:
[self performSelector:#selector(GameViewController) withObject:nil afterDelay:1];
hope someone knows bye thanks
EDIT explaining my code more
-(IBAction)play {
[self performSelector:#selector(delay) withObject:nil afterDelay:5.0];
play.enabled=NO;
UIImage *img = [UIImage imageNamed:#""];
if (imageview7.image == nil) {
if([textview.text isEqualToString:labelsText.text]){
-(void)delay {
[imageview7 setImage:imageview1HG.image];
[imageview8 setImage:imageview2HG.image];
[imageview9 setImage:imageview3HG.image];
[imageview10 setImage:imageview4HG.image];
[imageview11 setImage:imageview5HG.image];
[imageview12 setImage:imageview6HG.image];}
}
else {
-(void)delay {
[imageview7 setImage:imageview1H.image];
[imageview8 setImage:imageview2H.image];
[imageview9 setImage:imageview3H.image];
[imageview10 setImage:imageview4H.image];
[imageview11 setImage:imageview5H.image];
[imageview12 setImage:imageview6H.image];}
I guess you want to setImage on UIImageView one after the other after a delay.Here is the code for it.
Definition
#interface ViewController : UIViewController
{
NSMutableArray * imageViews;
NSMutableArray * images;
int count;
NSTimer * timer1;
}
-(void)setImageForView:(UIImageView *)imageView withImage:(UIImage *)image;
-(void)update;
-(IBAction)setImageOnViewOneAfterOther;
#end
Definition
-(void)setImageForView:(UIImageView *)imageView withImage:(UIImage *)image{
[imageView setImage:image];
}
-(void)update{
if (count < [imageViews count]) {
[self setImageForView:[imageViews objectAtIndex:count] withImage:[images objectAtIndex:count]];
count ++;
}else{
[timer1 invalidate];
}
}
-(IBAction)setImageOnViewOneAfterOther{
count = 0;
imageViews = [[NSMutableArray alloc] initWithObjects:img1v,img2v,img3v,img4v,img5v,img6v, nil]; // Set of all UIImageViews
images = [[NSMutableArray alloc] initWithObjects:img1,img2,img3,img4,img5,img6, nil];//Set of all UIImage's where img = [UIImage imageNamed:#"someImage1.png"];
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(update) userInfo:nil repeats:YES];
}
Why not animate it?
UIImageView* animatedImageView = [[UIImageView alloc]init];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
[UIImage imageNamed:#"4.gif"], nil]; //etc
animatedImageView.animationDuration = float([animatedImageView.animationImages count]);
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];

iPhone View Animation plays 2 ways on Simulator, only 1 way on Device

I have an odd question here. I have two animations set up for a simple page flip. I then have a button that randomly generates a 1 or a 0. If the number is 0 then it performs one animation, if its 1 it performs another.
The issues I'm seeing is that this all runs fine on the simulator, but on the device it doesn't perform the animation if the random number is 0.
Any clues?
-(IBAction)pageTurn:(id)sender {
int randomNumber = arc4random() %2;
NSLog(#"randomNumber = %d", randomNumber);
if (randomNumber == 0) {
[self turnPageForward];
} else {
[self turnPageBackward];
}
}
-(void)turnPageForward {
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"PageTurnOne.png"],
[UIImage imageNamed:#"PageTurnTwo.png"],
[UIImage imageNamed:#"PageTurnThree.png"],
[UIImage imageNamed:#"PageTurnFour.png"],
[UIImage imageNamed:#"PageTurnFive.png"],
[UIImage imageNamed:#"PageTurnSix.png"],
[UIImage imageNamed:#"PageTurnSeven.png"],
[UIImage imageNamed:#"PageTurnEight.png"],
[UIImage imageNamed:#"PageTurnNine.png"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(0, 0, 320, 480)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = .5; // seconds
myAnimatedView.animationRepeatCount = 1; // 0 = loops forever
[myAnimatedView startAnimating];
[self.view addSubview:myAnimatedView];
[myAnimatedView release];
}
-(void)turnPageBackward {
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"PageTurnNine.png"],
[UIImage imageNamed:#"PageTurnEight.png"],
[UIImage imageNamed:#"PageTurnSeven.png"],
[UIImage imageNamed:#"PageTurnSix.png"],
[UIImage imageNamed:#"PageTurnFive.png"],
[UIImage imageNamed:#"PageTurnFour.png"],
[UIImage imageNamed:#"PageTurnThree.png"],
[UIImage imageNamed:#"PageTurnTwo.png"],
[UIImage imageNamed:#"PageTurnOne.png"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(0, 0, 320, 480)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = .5; // seconds
myAnimatedView.animationRepeatCount = 1; // 0 = loops forever
[myAnimatedView startAnimating];
[self.view addSubview:myAnimatedView];
[myAnimatedView release];
}
Usually, when code fails on the device but not the simulator (or vice versa) the culprit is a library or other compiled source that was compiled for one hardware but not the other.
Another related possibility is that a resource was not added properly so that it is not included in the final build for the device. I would check the image files in the method that does not work.
You should also confirm if the turnPageForward is being called at all. That will give you a clue as to where the problem lays.

load animationImage from a table view Controller

I use a UITableViewController to display a list of items, and one of the items selection must lead to a
photo gallery created with animationImages. I usually declare the controllers associated to
the first level selection with something like :
MyController *myController = [[MyController alloc] initWithStyle:UITableViewStylePlain];
myController.title = #"title 1";
myController.rowImage = [UIImage imageNamed:#"myControllerIcon.png"];
..
But I believe a UIViewController is needed to use animationImages..
How can I display a image animation directly from a table view ?
If you have you image sequence build in this manner:
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"myImage1.png"],
[UIImage imageNamed:#"myImage2.png"],
[UIImage imageNamed:#"myImage3.png"],
[UIImage imageNamed:#"myImage4.gif"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:[self bounds]];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25; // seconds
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];
[myAnimatedView release];
Snatched from here
Then you can build your own cell or add the animationImage to the cell by adding it to either:
[cell setImage:myAnimatedView];
[cell setBackgroundView:myAnimatedView];
[cell setSelectedBackgroundView:myAnimatedView]

Am trying to add an animation to a class that has extended UIView, but it is not adding it at all!

This is the strangest error i have come across.
Am trying to add a countdown animation onto a Class that extends UIView. The code is shown below:
NSArray *myImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed:#"3.png"], [UIImage imageNamed:#"2.png"], [UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"Go.png"], nil];
UIImageView *myAnimatedView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 80, 80)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 3;
myAnimatedView.animationRepeatCount = 0;
[self addSubview:myAnimatedView];
[myAnimatedView startAnimating];
It is just not appearing on the screen. Any Ideas?
Have you made sure those images are added to your project?
Can you add them as a flat image (non-animated) -- as in:
UIImageView *myAnimatedView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"3.png"]];
[self addSubview:myAnimatedView];
(just to make sure that works first)?
If, instead of extending UIView, you extended UIImageView, would that make a difference?
Try switching the last 2 lines. From:
[self addSubview:myAnimatedView];
[myAnimatedView startAnimating];
To:
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];