Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-29-2011, 07:20 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 10
ZachBamberger is on a distinguished road
Unhappy Slight organization of coding I think??

So basically I have this app up and running. It's a very basic easy app a ball bounces around the screen and I have a "Start" and "Stop" switch. The "Start" plays an animation of images really quickly. "Stop" stops it.
What I am trying to figure out is how to connect the UIImage movement to the "Start" switch.


Code:
-(IBAction)play {
    animation.animationImages = [NSArray arrayWithObjects: 
                                 [UIImage imageNamed:@"blue.tiff"], 
                                 [UIImage imageNamed:@"orange.tiff"], 
                                 [UIImage imageNamed:@"pink.tiff"], 
                                 [UIImage imageNamed:@"green.tiff"],
                                 [UIImage imageNamed:@"purple.tiff"],
                                 [UIImage imageNamed:@"red.tiff"],
                                 [UIImage imageNamed:@"yellow.tiff"], nil];
    [animation setAnimationRepeatCount: -1];
    animation.animationDuration = .6;
    [animation startAnimating];
}
                                
-(IBAction)stop {
    [animation stopAnimating];

Code:
- (void)viewDidLoad
{
    position = CGPointMake(10, 3);
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
    [super viewDidLoad];
I have been trying to come up with an If statement but everything I try keeps coming up with errors, I don't think it's organized correctly.
ZachBamberger is offline   Reply With Quote
Old 11-29-2011, 07:23 PM   #2 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 10
ZachBamberger is on a distinguished road
Default

WAIT! I figured it out... well halfly...


Code:
-(IBAction)play {
    animation.animationImages = [NSArray arrayWithObjects: 
                                 [UIImage imageNamed:@"blue.tiff"], 
                                 [UIImage imageNamed:@"orange.tiff"], 
                                 [UIImage imageNamed:@"pink.tiff"], 
                                 [UIImage imageNamed:@"green.tiff"],
                                 [UIImage imageNamed:@"purple.tiff"],
                                 [UIImage imageNamed:@"red.tiff"],
                                 [UIImage imageNamed:@"yellow.tiff"], nil];
    [animation setAnimationRepeatCount: -1];
    animation.animationDuration = .6;
    [animation startAnimating];
    position = CGPointMake(10, 3);
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
    [super viewDidLoad];
}
But I still don't know how to get it to stop. When I push stop....
Hold on...
ZachBamberger is offline   Reply With Quote
Old 11-29-2011, 07:25 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by ZachBamberger View Post
So basically I have this app up and running. It's a very basic easy app a ball bounces around the screen and I have a "Start" and "Stop" switch. The "Start" plays an animation of images really quickly. "Stop" stops it.
What I am trying to figure out is how to connect the UIImage movement to the "Start" switch.


Code:
-(IBAction)play {
    animation.animationImages = [NSArray arrayWithObjects: 
                                 [UIImage imageNamed:@"blue.tiff"], 
                                 [UIImage imageNamed:@"orange.tiff"], 
                                 [UIImage imageNamed:@"pink.tiff"], 
                                 [UIImage imageNamed:@"green.tiff"],
                                 [UIImage imageNamed:@"purple.tiff"],
                                 [UIImage imageNamed:@"red.tiff"],
                                 [UIImage imageNamed:@"yellow.tiff"], nil];
    [animation setAnimationRepeatCount: -1];
    animation.animationDuration = .6;
    [animation startAnimating];
}
                                
-(IBAction)stop {
    [animation stopAnimating];

Code:
- (void)viewDidLoad
{
    position = CGPointMake(10, 3);
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
    [super viewDidLoad];
I have been trying to come up with an If statement but everything I try keeps coming up with errors, I don't think it's organized correctly.
You have written IBAction methods that start and stop the animation. Those should work perfectly attached to the "touch up inside" event on a pair of buttons.

Are you saying that you want a single switch that turns on the animation if it is in the on position and off if it is in the "off" position?

In that case, you'd write an IBAction like this:

Code:
- (IBAction) switchValueHasChanged: (id) sender;
{
  UISwitch* theSwitch = (UISwitch*) sender;
  if (theSwitch.on)
  {
    [self play]; //Call the method you already wrote
  }
  else
  {
    [self stop]; //Call the stop method you already wrote.
  }
}
Attach the switchValueHasChanged IBAction to the valueChanged event for the switch. Done.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-29-2011, 07:34 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 10
ZachBamberger is on a distinguished road
Default

It's not a switch button. But a round rect button.
I have it linked up. I think I got it.

Code:
-(IBAction)play {
    animation.animationImages = [NSArray arrayWithObjects: 
                                 [UIImage imageNamed:@"blue.tiff"], 
                                 [UIImage imageNamed:@"orange.tiff"], 
                                 [UIImage imageNamed:@"pink.tiff"], 
                                 [UIImage imageNamed:@"green.tiff"],
                                 [UIImage imageNamed:@"purple.tiff"],
                                 [UIImage imageNamed:@"red.tiff"],
                                 [UIImage imageNamed:@"yellow.tiff"], nil];
    [animation setAnimationRepeatCount: -1];
    animation.animationDuration = .6;
    [animation startAnimating];
    position = CGPointMake(10, 3);
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
    [super viewDidLoad];
}
                                
-(IBAction)stop {
    [animation stopAnimating];
    position = CGPointMake(10, 3);
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
}
Now when I run app, the image is frozen where I placed it in the storyboard. and when I hit start it starts moving along with the back ground animations.
But I am now trying to figure out the values to change for the -IBAction Stop
ZachBamberger is offline   Reply With Quote
Old 11-29-2011, 07:34 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 10
ZachBamberger is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
You have written IBAction methods that start and stop the animation. Those should work perfectly attached to the "touch up inside" event on a pair of buttons.

Are you saying that you want a single switch that turns on the animation if it is in the on position and off if it is in the "off" position?

In that case, you'd write an IBAction like this:

Code:
- (IBAction) switchValueHasChanged: (id) sender;
{
  UISwitch* theSwitch = (UISwitch*) sender;
  if (theSwitch.on)
  {
    [self play]; //Call the method you already wrote
  }
  else
  {
    [self stop]; //Call the stop method you already wrote.
  }
}
Attach the switchValueHasChanged IBAction to the valueChanged event for the switch. Done.
It's not a switch button. But a round rect button.
I have it linked up. I think I got it.

Code:
Code:
-(IBAction)play {
    animation.animationImages = [NSArray arrayWithObjects: 
                                 [UIImage imageNamed:@"blue.tiff"], 
                                 [UIImage imageNamed:@"orange.tiff"], 
                                 [UIImage imageNamed:@"pink.tiff"], 
                                 [UIImage imageNamed:@"green.tiff"],
                                 [UIImage imageNamed:@"purple.tiff"],
                                 [UIImage imageNamed:@"red.tiff"],
                                 [UIImage imageNamed:@"yellow.tiff"], nil];
    [animation setAnimationRepeatCount: -1];
    animation.animationDuration = .6;
    [animation startAnimating];
    position = CGPointMake(10, 3);
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
    [super viewDidLoad];
}
                                
-(IBAction)stop {
    [animation stopAnimating];
    position = CGPointMake(10, 3);
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
}
Now when I run app, the image is frozen where I placed it in the storyboard. and when I hit start it starts moving along with the back ground animations.
But I am now trying to figure out the values to change for the -IBAction Stop
If I put the selector as "nil" it will give me SIGBRT error.
ZachBamberger is offline   Reply With Quote
Old 11-29-2011, 07:40 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by ZachBamberger View Post
It's not a switch button. But a round rect button.
I have it linked up. I think I got it.

Code:
-(IBAction)play {
    animation.animationImages = [NSArray arrayWithObjects: 
                                 [UIImage imageNamed:@"blue.tiff"], 
                                 [UIImage imageNamed:@"orange.tiff"], 
                                 [UIImage imageNamed:@"pink.tiff"], 
                                 [UIImage imageNamed:@"green.tiff"],
                                 [UIImage imageNamed:@"purple.tiff"],
                                 [UIImage imageNamed:@"red.tiff"],
                                 [UIImage imageNamed:@"yellow.tiff"], nil];
    [animation setAnimationRepeatCount: -1];
    animation.animationDuration = .6;
    [animation startAnimating];
    position = CGPointMake(10, 3);
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
    [super viewDidLoad];
}
                                
-(IBAction)stop {
    [animation stopAnimating];
    position = CGPointMake(10, 3);
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
}
Now when I run app, the image is frozen where I placed it in the storyboard. and when I hit start it starts moving along with the back ground animations.
But I am now trying to figure out the values to change for the -IBAction Stop
Use 2 buttons, a stop button and a start button. Attach each one to it's action, and you're done.

Are you saying that you want a single button that toggles back and forth from a start button to a stop button?

If so, set up a BOOL instance variable animationIsPlaying.

Write a single IBAction method like the one I suggested, only instead of checking the state of a switch, have your action method toggle the state of the animationIsPlaying flag, and then call a method that:
  • Sets the button title to "start" or "stop" as appropriate based on the flag
  • Calls the animation stop or start method, as appropriate.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-29-2011, 07:54 PM   #7 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 10
ZachBamberger is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Use 2 buttons, a stop button and a start button. Attach each one to it's action, and you're done.

Are you saying that you want a single button that toggles back and forth from a start button to a stop button?

If so, set up a BOOL instance variable animationIsPlaying.

Write a single IBAction method like the one I suggested, only instead of checking the state of a switch, have your action method toggle the state of the animationIsPlaying flag, and then call a method that:
  • Sets the button title to "start" or "stop" as appropriate based on the flag
  • Calls the animation stop or start method, as appropriate.
I have two buttons a start and a stop. Each button starts and stops the background animation. The ball starts with the start button as well by coding.
But it doesn't stop with the stop button because I don't know what variables to change in the
Code:
  [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
ZachBamberger is offline   Reply With Quote
Old 11-29-2011, 08:08 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by ZachBamberger View Post
I have two buttons a start and a stop. Each button starts and stops the background animation. The ball starts with the start button as well by coding.
But it doesn't stop with the stop button because I don't know what variables to change in the
Code:
  [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
Background animation? Timer? You need to provide a complete, coherent description of what you are trying to do if you want help figuring out. Thus far your descriptions have been neither.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Reply

Bookmarks

Tags
code, if else, organization

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 406
16 members and 390 guests
AppsBlogger, chiataytuday, Clouds, David-T, dedeys78, Duncan C, e2applets, EvilElf, iekei, ipodphone, leostc, LunarMoon, Murphy, sacha1996, Sami Gh, teebee74
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,912
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:40 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0