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 07-24-2009, 09:39 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 7
syler is on a distinguished road
Unhappy Animate Dynamically created buttons

I'm dynamically creating UIButton's in my app. When I show the buttons I want to animate them. I can't seem to get the buttons to animate with this code:

Code:
UIImage *buttonBackground = [UIImage imageNamed:@"ButtonRed.png"];
for (NSInteger x = 0; x < 4; x++) {

    CGRect btnFrame = CGRectMake(x * (buttonBackground.size.width+2),
                                 y * (buttonBackground.size.height + 1),
                                 buttonBackground.size.width,
                                 buttonBackground.size.height);
    UIButton *gridButton = [[[UIButton alloc] initWithFrame: btnFrame] retain];

    [gridButton setBackgroundImage:buttonBackground forState:UIControlStateNormal];
    [buttonBackground release];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:gridButton cache:YES];

    [self.view addSubview:gridButton];

    [UIView commitAnimations];
    [gridButton release];
}
If I change the "forView:gridButton" to "forView:self.view", then the whole view flips but but not the individual buttons. I just want each individual button to flip.

Thanks for any help.
syler is offline   Reply With Quote
Old 07-27-2009, 06:46 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 7
syler is on a distinguished road
Default I figured it out with some help from a stackoverflow.com user's help...

It seemed as though the animations would not run inside the for loop for each individual button. I tried to wrap the whole FOR loop in a beginAnimations call, but that didn't work either.

I was finally able to animate the buttons by setting all the dynamically created buttons to be hidden at first in the FOR loop and then just outside the FOR loop I created a timer that would unhide them while doing the animation.

Here's the code changes I made:


Code:
    UIImage *buttonBackground = [UIImage imageNamed:@"ButtonRed.png"];
    for (NSInteger x = 0; x < 4; x++) {
    
        CGRect btnFrame = CGRectMake(x * (buttonBackground.size.width+2),
                                     y * (buttonBackground.size.height + 1),
                                     buttonBackground.size.width,
                                     buttonBackground.size.height);
        UIButton *gridButton = [[[UIButton alloc] initWithFrame: btnFrame] retain];
    
        [gridButton setBackgroundImage:buttonBackground forState:UIControlStateNormal];
        [buttonBackground release];
    
        // ****** I took out all animation code ******
        [self.view addSubview:gridButton];
        gridButton.hidden = YES; // <----------- ****** new code ******
    
        [gridButton release];
    }

    // **** the following code was added just outside the FOR loop to create timer that will animate buttons as they're un-hidden.
    // NSTimer gridTimer is created in the header file.
	gridTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(showGrid) userInfo:nil repeats:NO];
Here's the new method I call with the timer:

Code:
    -(void) showGrid{
    	for (UIButton *obj in self.view.subviews) {
    		if ([obj isMemberOfClass:[UIButton class]]) {
    			[UIView beginAnimations:nil context:NULL];
    			[UIView setAnimationDuration:1];
    			[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:obj cache:YES];//~~self.view cache:YES];				
    			obj.hidden = NO;
    			[UIView commitAnimations];
    		}
    	}
    	[gridTimer invalidate];
    }
syler is offline   Reply With Quote
Reply

Bookmarks

Tags
beginanimations, button, dynamic, iphone, uibutton

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: 320
11 members and 309 guests
chiataytuday, coolman, givensur, guusleijsten, ipodphone, jbro, mediaspree, mottdog, mtl_tech_guy, Punkjumper, vilisei
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,882
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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