★ TouchArcade needs your help. Click here to support us on Patreon.

2 quick questions!

03-26-2009, 09:21 PM
#1
Joined: Jan 2009
Location: App Tech Studios, USA
Posts: 1,363
2 quick questions!

I have 2 quick simple questions. 1. How do you make a view re-load? eg: When I go from view 1 to view 2 (view 2 has animation) and then I go back to view 1. And then back to view 2. Now the animation that was going hasn't restarted, it was going the whole time while I was back in view 1. So if it was going across the screen, it is now gone off the screen.


2. This is very simple, how do I make a view that has something start after a certain amount of time? eg: I set a timer(NSTimer?) to 30 seconds and after 30 seconds my animation (or anything) starts.

I know that these are basic, but I just never caught these things.

Thank you!
03-26-2009, 09:39 PM
#2
Joined: Dec 2008
Posts: 439
Do you use a method in view 2 to start the animation... call that method when you switch views...

can you post the code showing how you switch views?

For the second question use:

Code:
(NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
so something like this:

[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(nameofyourmethod) userInfo:nil repeats:NO];

03-26-2009, 10:06 PM
#3
Joined: Jan 2009
Location: App Tech Studios, USA
Posts: 1,363
Quote:
Originally Posted by arkanigon View Post
Do you use a method in view 2 to start the animation... call that method when you switch views...

can you post the code showing how you switch views?

For the second question use:

Code:
(NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
so something like this:

[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(nameofyourmethod) userInfo:nil repeats:NO];
Thank you! I use self presentModalViewCOntroller:secondViewController:An imated:YES;
03-26-2009, 10:46 PM
#4
Joined: Dec 2008
Posts: 439
Quote:
Originally Posted by WellSpentYouth View Post
Thank you! I use self presentModalViewCOntroller:secondViewController:An imated:YES;
Right before that line... or right after it... can you do something like

[secondViewController startanimation];
03-27-2009, 10:07 AM
#5
Joined: Jan 2009
Location: App Tech Studios, USA
Posts: 1,363
So what I have is an animation that goes across the screen and when I tap it, it goes the other direction. I made it so when it goes off the screen it does NOT come back the other side. So what I have is this, when you open up view 2 for the first time, there is the animated thing and a button that says ready. When you tap ready, the animation starts. Now when I go back to view 1 and then go to view 2 again, the animation has run off the screen, so do you want me to put the animation code on view 1 and then attach the "ready" button code to the method that changes views? And then attach the code in view 1 for the UIButton, to the UIButton in view 2 (animation)? Thank you!

Would I have a method in the second view controller that was called "start animation"? So when I switched views I am calling that method?


Thank you!

EDIT: I tried doing [secondViewController tick]; right after the changing view stuff, but it did nothing and it said that it may not respond because it didn't have the matching id or something. And yes, tick is the correct method.

Last edited by WellSpentYouth; 03-27-2009 at 10:19 AM.
03-27-2009, 10:25 AM
#6
Quote:
Originally Posted by WellSpentYouth View Post
EDIT: I tried doing [secondViewController tick]; right after the changing view stuff, but it did nothing and it said that it may not respond because it didn't have the matching id or something. And yes, tick is the correct method.
The exact error message is always important.

It sounds to be like you just need to #import the secondViewController header file into the file that calls [secondViewController tick].

Didev Studios || Twitter || YouTube || Facebook
Releases
GeoSnake || Bugz || Review Scraper (Open Source)
03-27-2009, 11:20 AM
#7
Joined: Jan 2009
Location: App Tech Studios, USA
Posts: 1,363
Quote:
Originally Posted by InsertWittyName View Post
The exact error message is always important.

It sounds to be like you just need to #import the secondViewController header file into the file that calls [secondViewController tick].
It says "Second view controller may not respond to 'tick'". And then underneath it says "messages without a matching id will be assumed to return id and accept arguments. Thank you! And no, when I imported the .h file, it still said the same warning.



EDIT: I got it to start the animation when the view is switched, but when I exit the view and then come back to it, the animation is still gone. AND, I want the animation to wait until I press the ready button. So now I am worse off, not only does it not restart when the view is exited and then opened, but now it starts the second the view is loaded. All I want is for the animations to reset to where they were the first time that the view is opened. Otherwise, when I re-open the view, I can't see the animation because it went off the screen. Thank you!!!

Last edited by WellSpentYouth; 03-27-2009 at 11:26 AM.
03-27-2009, 11:24 AM
#8
Can you paste the function prototype for the tick method? it'll be part of your secondViewController's @implementation.

Didev Studios || Twitter || YouTube || Facebook
Releases
GeoSnake || Bugz || Review Scraper (Open Source)
03-27-2009, 11:30 AM
#9
Joined: Jan 2009
Location: App Tech Studios, USA
Posts: 1,363
So this is the tick method (tick1 or tick2) and here is the code for the animation. There is also an IBAction that makes them go left or right, but that isn't necessary. I just want the page to re-load to how it was the first time I load it. Thank you!


- (void)tick1{
//This method is invoked by the NSTimer every 1/5 second
if(shouldRunLeft1)
[self picRunLeft1];
else
[self picRunRight1];
}

- (void)picRunRight1{//The code in this method controls a simple sprite animation loop
UIImage *piImage1 = [UIImage imageNamed:@"stick.png"];
UIImage *piImage2 = [UIImage imageNamed:@"stick1.png"];

//Notice since we are using a custom UIButton, we cannot call pig.image =
[UIView setAnimationTransition:UIViewAnimationTransitionNo ne forViewi cache:YES];

CGPoint center = pi.center;
center.x += 10;


pi.center = center;
[UIView commitAnimations];


if(pi.currentImage == piImage1)
[pi setImageiImage2 forState:UIControlStateNormal];

else
[pi setImageiImage1 forState:UIControlStateNormal];
}

- (void)picRunLeft1{

UIImage *piImage1 = [UIImage imageNamed:@"stick.png"];
UIImage *piImage2 = [UIImage imageNamed:@"stick1.png"];

[UIView setAnimationTransition:UIViewAnimationTransitionNo ne forViewi cache:YES];

CGPoint center = pi.center;
center.x -= 10;


pi.center = center;
[UIView commitAnimations];

if(pi.currentImage == piImage1)
[pi setImageiImage2 forState:UIControlStateNormal];

else
[pi setImageiImage1 forState:UIControlStateNormal];

}
03-27-2009, 11:35 AM
#10
Sorry, I meant the @interface, not the @implementation, ie. in the header file.

If you don't have a prototype there, that'll be why you get the error.

To clarify, in your viewDidLoad method (or wherever) you'd allocate as normal then in your viewWillAppear method set the objects default values.

You can also use the viewDidDisappear method, if you need to do anything like invalidate timers etc.

This should work so that whenever the view is about to appear, it resets everything as needed.

Make sense?

Didev Studios || Twitter || YouTube || Facebook
Releases
GeoSnake || Bugz || Review Scraper (Open Source)

Last edited by InsertWittyName; 03-27-2009 at 11:44 AM.