How to prevent UIImage from continually replaying an animation - iphone

I am making a game and currently my "player" is animating as I've implemented (which is wrong at the moment) and it continually runs the animation from start to start because it always restarts from the beginning when the function is called. What is the best way to prevent this and let the animation run all the way through? Here is the code I'm using at the moment:
- (void) startAnimatingLeft
{
NSArray *images = [NSArray arrayWithObjects:img9,img10,img11,img12,img13,img14,img15,img16, nil];
[imageView setAnimationImages:images];
[imageView startAnimating];
animateTimer = [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:#selector(nothingMovingLeft) userInfo:nil repeats:NO];
}

From UIImage documentation:
startAnimating
This method always starts the animation from the first image in the list.
So you shouldn't call this method if the image is already animating. You can call isAnimating method to check if image is animating.

Related

Why does my UIImage not display in a routine other than ViewDidLoad()?

I am running XCOde 5, ios7.
I created a UIImage in the Storyboard.
If I set the .image property using:
self.image43.image = [UIImage imageNamed:self.string4];
in ViewDidLoad(), it WORKS FINE!
If, however, execute the exact same code inside my gameplay routine (the routine have setup as my timer selector as shown below), it does not display.
self.myTImer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector #selector(gameplay:) userInfo:nil repeats:YES
I'm sooooo close to being done..... :)
THanks
-Ed

Capture frames with time delay - iPhone camera

I'm trying to write some objective-c code that allows capture of frames through the iPhone camera, after a specific time delay. Any examples I have found online have captured the 'present' frame input by the camera (i.e. a realtime frame capture). I am looking for a less real time frame capture - a way to capture frames with a specific time delay. Any ideas?
I would use NSTimer.
It would allow you to use the code that captures the current frame, and it would get called after a delay.
You can create a non-repeating timer like this: timer = [NSTimer scheduledTimerWithTimerInterval:delay target:self selector:#selector(capture) userInfo:nil repeats:NO]; where delay is the specific time delay and capture is a method that contains the code from an example that captures the present frame.
Use following code.....
//call method according to your delay that you need.....
float yourdelay;
yourdelay = 0.7;
[self performSelector:#selector(captureView) withObject:nil afterDelay:yourdelay];
// or set time for calling method....
[NSTimer scheduledTimerWithTimeInterval:0.02 target: self selector:#selector(captureView) userInfo:nil repeats:NO];
///or you can call method directly when you need..
[self captureView];
////Here the code for capture real time image.
- (void)captureView {
UIGraphicsBeginImageContext(mapView.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
flickerImaveView.image=viewImage;
}
Hope this will help you....

In objective c Xcode Changing UIImages at the push of a button

When i push the button I would like the first image to show in the UIImageView, stay for a short period of time and then the next image to show. Only the second image shows after a period of time. The first image never comes on.
// TestProjectViewController.m
// Created by Jack Handy on 3/8/12.
#import "TestProjectViewController.h"
#implementation TestProjectViewController
#synthesize View1= _view1;
#synthesize yellowColor = _yellowColor;
#synthesize greenColor = _greenColor;
- (IBAction)button:(id)sender {
_greenColor = [UIImage imageNamed: #"green.png"];
_view1.image = _greenColor;
[NSThread sleepForTimeInterval:2];
_yellowColor = [UIImage imageNamed: #"yellow.png"];
_view1.image = _yellowColor;
}
#end
U can try and place the
_yellowColor = [UIImage imageNamed: #"yellow.png"];
_view1.image = _yellowColor;
and instead of the
[NSThread sleepForTimeInterval:2];
call this one
[self performSelector:#selector(changeColor) withObject:nil afterDelay:2];
The problem here is that you are replacing the image before the OS gets a chance to draw. Since all of these three operations: change the image, wait 2 seconds, change the image again) occur before your button action returns, you are preventing the Main thread from executing and thus the screen from refreshing. So, what's happening is that after 2 seconds, the screen draws with the image you most recently put in its place.
You need to have the waiting happen separately. There are three typical ways to do this, each of which have their benefits:
- send yourself a delayed message using -performSelector:withObject:afterDelay:
- spawn another thread or use a dispatch queue to run a thread in the background for the sleep and then send the message to the main thread from there
- or, use a timer.
My suggestion would be to use the timer, because it is easily cancelable if you need to do something like move to another screen.
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target: self selector: #selector(updateColor:) userInfo: nil repeats: NO];
// store the timer somewhere, so that you can cancel it with
// [timer invalidate];
// later as necessary
and then later:
-(void)updateColor:(NSTimer*)timer
{
_yellowColor = [UIImage imageNamed: #"yellow.png"];
_view1.image = _yellowColor;
}
If you want the colors to alternate, you can pass YES for the repeats: value in the creation code and then change -updateColor: to alternate... or move to the next color.

Fetching image From Animating UIImageView

I'm using animation method of for implementing slide show in UIimageView as:
mainSlideShowImageView.animationImages=images;
mainSlideShowImageView.animationDuration = 75.00;
mainSlideShowImageView.animationRepeatCount = 0; //infinite
[mainSlideShowImageView startAnimating];
Now, what I want is to know which image is currently on image view screen.
How can I do so?
If it is not possible please tell that too.
Edit: 'images' here is array of UIImage.
AFAIK, there is no method which returns imageview's current image , however I have created a workaround for this....
array= [NSArray arrayWithObjects:[UIImage imageNamed:#"1.jpg"],[UIImage imageNamed:#"2.jpg"],[UIImage imageNamed:#"3.jpg"],[UIImage imageNamed:#"4.jpg"],[UIImage imageNamed:#"5.jpg"],nil];
mainSlideShowImageView.animationImages= array;
mainSlideShowImageView.animationDuration=15.0;
i=0;
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(increase) userInfo:nil repeats:YES];
[mainSlideShowImageView startAnimating];
-(void)increase
{
i++;
if (i>4) {
i=0;
}
}
-(void)getimage
{
UIImage* im = [array objectAtIndex:i];
// im is the current image of imageview
}
I have taken 5 images here and animation duration of 15 , that means image will change at every 3 seconds, so I have kept timer to fire at every 3 seconds ... and since array contains 5 images ... so if 'i' goes beyond 4 I have put it back to 0 in method increase

Release NSObject and stop all method calling

So my menu calls a game with this piece of code:
game = [[Game alloc] init];
[self presentModalViewController:memoryTest animated:FALSE];
A UIViewController then appears with a countdown. The player can go back to the menu DURING the countdown. However when I try this, the countdown keeps running and eventually the game starts, even thought the UIViewController has been dismissed (therefore the UIView has disappeared) in the backToMenu method.
[self.parentViewController dismissModalViewControllerAnimated:FALSE];
I've tried to release the game object in the viewDidAppear method of the menu, but no luck. I thought of having a "quit" BOOL value, so the countdown can check wether or not the player has quit the game, but there must be a better way to release an object AND stop all method calls inside it.
Thanks for helping me out.
CountDown method:
- (void)countDown {
SoundEffect *sound = [[SoundEffect alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"tick" ofType: #"wav"]];
[sound playAndRelease];
if(self.countDownStep > 0) {
feedback.image = [UIImage imageNamed:[NSString stringWithFormat:#"countdown%d.png",self.countDownStep]];
feedback.hidden = FALSE;
[self performSelector:#selector(countDown) withObject:nil afterDelay:0.8];
}
else {
[self displayMessage:self.startMessage];
[self.game performSelector:#selector(start) withObject:nil afterDelay:0.8];
[self performSelector:#selector(hide) withObject:nil afterDelay:0.8];
}
self.countDownStep--;
}
In the viewcontroller's viewWillDisappear, check if the timer is running. If it is, then just invalidate it.
[timerObject invalidate] will stop the timer
How do you handle the countdown? It seems like you want to explicitly void that countdown in the viewWillDisappear method for the UIViewController you mentioned.
I was assuming you would use something like NSTimer. In fact, that might still not be a bad idea. NSTimer can handle re-running your countdown method every 0.8 seconds. If you decrement the value to 0, your countdown has expired. If your view disappears, invalidate the timer in viewWillDisappear.