2010-12-20 10 views
10

Estoy tratando de animar una propiedad tintColor de UIToolbar, para cambiarla de un tintColor a otro.¿Puedo fundir/animar el tintColor de una UIToolbar?

Aquí está el código que estoy intentando. Desafortunadamente, el cambio ocurre inmediatamente y no se desvanece de verde a azul. Esto es extraño porque sé que Apple se desvanece y "pulsa" los colores de la tonalidad de la barra de herramientas al anclarse o en una llamada telefónica. Entonces, ¿por qué esto no funciona?

// set initial tint color 
myBottomToolBar.tintColor = [UIColor colorWithRed:0.15 green:0.95 blue:0.15 alpha:0.6]; 

//animation stuff 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.95]; 
[UIView setAnimationDelegate:self];   

//thing to animate 
myBottomToolBar.tintColor = [UIColor colorWithRed:0.15 green:0.35 blue:0.45 alpha:0.6]; 

//animation stuff 
[UIView commitAnimations]; 
+0

Intente pegar su código a través del botón binario en el editor WSYIWYG. De esa manera mantiene su formateo. – TALLBOY

Respuesta

3

El color del tinte no se puede animar a través de API públicas. Puede solucionar esto cambiando manualmente el color del tinte en un temporizador. Tendría que interpolar los niveles de color intermedios.

0

Para referencia futura, aquí está mi solución algo apresurada para animar el tinte. Estoy seguro de que hay formas más inteligentes de hacer esto con las categorías y una estructura para los valores rgb yadda yadda. Tenía prisa, ¿de acuerdo?

UITabBarController subclase:

@interface BaseNavigationController : UINavigationController 
{ 
    NSTimer *   tintTimer; 
    UIColor *   targetColor; 
} 

-(void)changeTintTo:(UIColor*)color; 
-(void)animateTint; 

.

-(void)changeTintTo:(UIColor*)color; 
{ 
    targetColor = [color retain]; 

    [tintTimer invalidate]; 
    tintTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(animateTint) userInfo:nil repeats:YES]; 

} 

-(void)animateTint; 
{ 
    UIColor * currentColor = self.navigationBar.tintColor; 

    CGFloat red, green, blue, alpha; 
    [currentColor getRed:&red green:&green blue:&blue alpha:&alpha]; 

    CGFloat targetRed, targetGreen, targetBlue, targetAlpha; 
    [targetColor getRed:&targetRed green:&targetGreen blue:&targetBlue alpha:&targetAlpha]; 

    CGFloat newRed = red + ((targetRed - red)*.2); 

    UIColor * newColor = [UIColor colorWithRed:newRed 
             green:green + ((targetGreen - green)*.2) 
              blue:blue + ((targetBlue - blue)*.2) // the .2 adjusts the fade speed 
             alpha:1.0]; 

    if(
     (newRed < targetRed && newRed >= targetRed-0.01) || 
     (newRed > targetRed && newRed <= targetRed+0.01) 
    ) 
    { 
     newColor = targetColor; 
     [targetColor autorelease]; 
     [tintTimer invalidate]; 
     tintTimer = nil; 
     targetColor = nil; 
    } 

    self.navigationBar.tintColor = newColor; 

} 
+0

Debe usar 'CADisplayLink' en lugar de' NSTimer' para las animaciones. – rounak

0

NSTimer y su instalación es mucho trabajo para una animación simple. Aquí está mi solución mediante el envío, sólo estoy un desvanecimiento UIBarButtonItem dentro y fuera cambiando el valor alfa del color de la tinta:

-(void)animateItemToTargetAlpha:(CGFloat)targetAlpha 
{ 
    static dispatch_source_t timer = nil; 
    static CGFloat DURATION = 0.25f; 
    static CGFloat FPS = 30.f; 
dispatch_source_cancel(timer); 
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()); 
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1/FPS * NSEC_PER_SEC,(1ull * NSEC_PER_SEC)/10); 

    CGFloat red, green, blue, __block alpha; 
    [self.navigationItem.rightBarButtonItem.tintColor getRed:&red green:&green blue:&blue alpha:&alpha]; 

    dispatch_source_set_event_handler(timer, ^{ 
     alpha = targetAlpha == 1.0f ? alpha + (1/(FPS * DURATION)) : alpha - (1/(FPS * DURATION)); 
     self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; 
     if(alpha >= 1 || alpha <= 0) 
     { 
      dispatch_source_cancel(timer); 
     } 
    }); 

    dispatch_resume(timer); 
} 

La curva lineal puede ser un poco notable, pero estoy interesado en probar primero CADisplayLink antes de hacer cualquier cambio

Cuestiones relacionadas