08-10-2010, 04:14 AM
#1 (permalink )
Registered Member
Join Date: Nov 2009
Posts: 65
Animating a UIButton fade in/out in a loop
Hi
I am going to end up with an array of RSS feeds, and would like a button or some such to display them at the bottom of the view. I would like to animate through each feed in the array.
This is what i have so far to animate, which, works for the fade, but only animates the last item of the array.
Code:
feed = [[UIButton alloc] initWithFrame:CGRectMake(0,380,320,43)];
[self.view addSubview:feed];
feed.alpha=1;
NSArray *feeds = [NSArray arrayWithObjects:[NSString stringWithFormat:@"1234567"],
[NSString stringWithFormat:@"qwerty"],[NSString stringWithFormat:@"asdfgh"],nil];
for (NSString* f in feeds){
[feed setTitle:f forState:UIControlStateNormal];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2.0f];
feed.alpha=0;
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
}
Im sure its simple.
Thanks
Joe
08-10-2010, 04:18 AM
#2 (permalink )
Registered Member
Join Date: Oct 2009
Location: Amsterdam, The Netherlands
Posts: 782
Code:
feed = [[UIButton alloc] initWithFrame:CGRectMake(0,380,320,43)];
[self.view addSubview:feed];
feed.alpha=1;
NSArray *feeds = [NSArray arrayWithObjects:[NSString stringWithFormat:@"1234567"],
[NSString stringWithFormat:@"qwerty"],[NSString stringWithFormat:@"asdfgh"],nil];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2.0f];
for (NSString* f in feeds){
[feed setTitle:f forState:UIControlStateNormal];
feed.alpha=0;
}
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
Try this.
08-10-2010, 04:25 AM
#3 (permalink )
Registered Member
Join Date: Nov 2009
Posts: 65
Thanks, I tried that, and it still seems to just animate the last item. Would i need an NSTimer as well?
08-10-2010, 04:28 AM
#4 (permalink )
Registered Member
Join Date: Oct 2009
Location: Amsterdam, The Netherlands
Posts: 782
It also looks like you just have one button called feed.
What you could do is in the animationDidStop set the next label via animantion.
And repeat until done.
This way you will not need a timer.
08-10-2010, 04:33 AM
#5 (permalink )
Registered Member
Join Date: Nov 2009
Posts: 65
I do have only one button called feed, i essentially just want to replace the title of the button, after fading the previous one out...
My animationDidStop method just looks like:
Code:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0f];
feed.alpha = 0;
[UIView commitAnimations];
What would i need to change in this?
Thanks.
08-10-2010, 04:41 AM
#6 (permalink )
Registered Member
Join Date: Oct 2009
Location: Amsterdam, The Netherlands
Posts: 782
Create a property for feeds array and a property with the current index.
Start with position 0 in your first call and set the current index to 0 and start the first animation
Then in the animation did stop, check if the current index is large then the count of the array.
If not up the curentindex with one and do the animation again.
08-10-2010, 04:56 AM
#7 (permalink )
Registered Member
Join Date: Nov 2009
Posts: 65
I have done this now, and it animates the first item twice now :s
Code:
if (!(currentIndex > [appDelegate.feeds count])) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationOptionCurveEaseIn];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
Feed *f = [appDelegate.feeds objectAtIndex:currentIndex];
[feed setTitle:f.author forState:UIControlStateNormal];
[feed addTarget:self action:@selector(popUpModal:) forControlEvents:UIControlEventTouchUpInside];
feed.alpha=1;
[UIView commitAnimations];
currentIndex++;
}
What have i missed? It's very early here
08-10-2010, 05:03 AM
#8 (permalink )
Registered Member
Join Date: Oct 2009
Location: Amsterdam, The Netherlands
Posts: 782
Looks correct to me.
08-10-2010, 05:04 AM
#9 (permalink )
Registered Member
Join Date: Nov 2009
Posts: 65
Should it be an if or while?
08-10-2010, 05:32 AM
#10 (permalink )
Registered Member
Join Date: Oct 2009
Location: Amsterdam, The Netherlands
Posts: 782
If should be just fine, because you call the function every time the animation is stopped.
08-10-2010, 05:36 AM
#11 (permalink )
Registered Member
Join Date: Nov 2009
Posts: 65
I understand that it should work, but it is behaving weirdly... I've never done animation before, so i am possibly missing something, either in the initial animation call or the didStop method... where do i set currentIndex to 0, before or after the initial animation ?
Thanks
08-10-2010, 05:41 AM
#12 (permalink )
Registered Member
Join Date: Nov 2009
Posts: 65
OK, so now i have got it Looping through them all, but it still does the first one twice i assume because in the first call when the index is 0, its still matching the if condition...
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 324
9 members and 315 guests
flamingliquid , ilmman , iram91419 , linkmx , nadav@webtview.com , Objective Zero , Paul Slocum , stanny , v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,656
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, iram91419