iphone - Animating a pulsing UILabel? -


i trying animate color the text on uilabel pulse from: [black] [white] [black] , repeat.

- (void)timerflash:(nstimer *)timer { [[self navtitle] settextcolor:[[uicolor whitecolor] colorwithalphacomponent:0.0]]; [uiview animatewithduration:1                       delay:0                      options:uiviewanimationoptionallowuserinteraction                   animations:^{[[self navtitle] settextcolor:[[uicolor whitecolor] colorwithalphacomponent:1.0]];}                   completion:nil]; } 

.

[self setfadetimer:[nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(timerflash:) userinfo:nil repeats:yes]]; 

firstly not sure of method, plan (as can see above) set animation block , call using repeating nstimer until canceled.

my second problem (as can see above) animating black (alpha 0) white (alpha 1) don't know how animate black again animation loops seamlessly

essentially want text color pulse on uilabel until user presses button continue.

edit_001:

i getting trouble because can't animate [uilabel setcolor:] can animated [uilabel setalpha:] going give go.

edit_002:

- (void)timerflash:(nstimer *)timer {     [[self navtitle] setalpha:0.5];     [uiview animatewithduration:2                            delay:0                          options:uiviewanimationoptionallowuserinteraction                       animations:^{[[self navtitle] setalpha:0.9];}                       completion:nil]; } 

this works (btw: want stop why hooked nstimer can cancel that) thing animates midgray nearwhite , pops back. know how animate nearwhite midgray nice smooth cycle? enter image description here

edit_003: (solution)

the code suggested dave delong (see below) indeed work when modified use calayer opacity style attribute:

uilabel *navtitle; @property(nonatomic, retain) uilabel *navtitle; 

.

// add animation cabasicanimation *anim = [cabasicanimation animationwithkeypath:@"opacity"]; [anim settimingfunction:[camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout]]; [anim setfromvalue:[nsnumber numberwithfloat:0.5]]; [anim settovalue:[nsnumber numberwithfloat:1.0]]; [anim setautoreverses:yes]; [anim setduration:0.5]; [[[self navtitle] layer] addanimation:anim forkey:@"flash"]; 

.

// remove animation [[[self navtitle] layer] removeanimationforkey:@"flash"]; 

you caanimation. pushed little demo project did while ago onto github. shows olympic logo, , makes rings fly around random location on screen, animate original position. probably similar, not using position property.

here's basic code make 1 of rings fly around:

cabasicanimation * = [cabasicanimation animationwithkeypath:@"position"]; [a settimingfunction:[camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout]]; [a setfromvalue:[nsvalue valuewithcgpoint:[layer position]]]; [a settovalue:[nsvalue valuewithcgpoint:[self randompoint]]]; [a setautoreverses:yes]; [a setduration:1.0]; [layer addanimation:a forkey:nil]; 

your fromvalue , tovalue values cgcolorrefs, , animation keypath different, trick here setautoreverses:yes. animate fromvalue tovalue, , fromvalue. it's pretty darn handy. :)

https://github.com/davedelong/demos/blob/master/olympicrings


if doesn't work (and might not), might layer 2 uilabels on top of each other , animate alpha values 0.0 1.0 (or vice versa) @ same time.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -