2010-08-04 26 views
6

Por lo tanto, actualmente estoy probando las animaciones UIView y noté que las sombras representadas por CALayer (usando [view.layer setShadowStuffHere]) desaparecen al comienzo de la animación y vuelven a aparecer al final de la animación. ¿Hay alguna forma de que pueda mantener estas sombras y animar las sombras junto con UIView? Intenté usar sombras sin caminos fronterizos, pero esa fue una idea terrible, ya que la velocidad de fotogramas cayó como una roca durante la animación, y no obtuve sombras de todos modos.CALayer Sombras desaparecen durante una animación UIView

El código que estoy utilizando ahora está debajo. Inicialmente verá una vista roja, y cuando se golpee, se convertirá en una vista azul más grande. El resultado final debería ser algo similar a la aplicación de música del iPad; cuando se selecciona un álbum, se voltea para revelar una vista en la parte posterior.

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UITapGestureRecognizer * tapRec; 

    // Drop shadow Path creation 
    CGFloat x1 = 0; 
    CGFloat y1 = 0; 
    CGFloat x2 = 100; 
    CGFloat y2 = 100; 

    frontBorderPath = CGPathCreateMutable(); 
    CGPathMoveToPoint(frontBorderPath, NULL, x1, y1); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x2, y1); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x2, y2); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x1, y2); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x1, y1); 

    x2 = 400; 
    y2 = 400; 
    backBorderPath = CGPathCreateMutable(); 
    CGPathMoveToPoint(backBorderPath, NULL, x1, y1); 
    CGPathAddLineToPoint(backBorderPath, NULL, x2, y1); 
    CGPathAddLineToPoint(backBorderPath, NULL, x2, y2); 
    CGPathAddLineToPoint(backBorderPath, NULL, x1, y2); 
    CGPathAddLineToPoint(backBorderPath, NULL, x1, y1); 

    containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; 
    containerView.clipsToBounds = NO; 
    [containerView.layer setShadowPath:frontBorderPath]; 
    [containerView.layer setShadowOpacity:1]; 
    [containerView.layer setShadowOffset:CGSizeMake(0,0)]; 
    [containerView.layer setShadowRadius:3]; 
    [containerView.layer setShouldRasterize:NO]; 
    [self.view addSubview:containerView]; 

    tapRec = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(frontTap)] autorelease]; 
    frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    frontView.backgroundColor = [UIColor redColor]; 
    [frontView addGestureRecognizer:tapRec]; 
    [containerView addSubview:frontView]; 

    tapRec = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backTap)] autorelease]; 
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)]; 
    backView.backgroundColor = [UIColor blueColor]; 
    [backView addGestureRecognizer:tapRec];  
} 

- (void)frontTap{ 
    NSLog(@"fronttap"); 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.7]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:containerView cache:YES]; 

    [frontView removeFromSuperview]; 
    [containerView addSubview:backView]; 
    containerView.frame = CGRectMake(100, 100, 400, 400); 
    [containerView.layer setShadowPath:backBorderPath]; 

    [UIView commitAnimations]; 

} 

- (void)backTap{ 
    NSLog(@"backtap"); 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.7]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:containerView cache:YES]; 

    [backView removeFromSuperview]; 
    [containerView addSubview:frontView]; 
    containerView.frame = CGRectMake(100, 100, 100, 100); 
    [containerView.layer setShadowPath:frontBorderPath]; 

    [UIView commitAnimations]; 

} 

Respuesta

12

Resulta que cuando estás haciendo una animación UIView, ignora de su vista clipsToBounds propiedad y los clips de todas maneras (al menos para las animaciones de tipo flip), por lo que si usted quiere que sus sombras para que esté siempre visible , necesitarás tener un marco de vista lo suficientemente grande como para contener todo.

+0

¿Puede por favor elaborar un poco? – sole007

+0

@ sole007: esta respuesta en particular es probablemente demasiado antigua para ser relevante hoy (y no he tenido ningún encontronazo reciente con este problema desde entonces), pero en caso de que sea necesario, el problema era que incluso si estaba configurado clipsToBounds a falso, y usaste CALayers para renderizar sombras fuera de los límites particulares de una vista, confiando en que clipsToBounds es falso para que aparezcan esas sombras, no importaría y esas sombras no se mostrarían. La solución es entonces que los límites de la vista contengan las sombras en lugar de las sombras que se encuentran fuera de los límites. –

Cuestiones relacionadas