2010-08-04 3 views
6

Je suis donc en train de tester les animations UIView, et j'ai remarqué que les ombres restituées par CALayer (en utilisant [view.layer setShadowStuffHere]) disparaissent au début de l'animation et réapparaissent à la fin de l'animation. Y a-t-il un moyen de garder ces ombres et d'animer les ombres avec UIView? J'ai essayé d'utiliser des ombres sans chemins de bordure, mais c'était une idée terrible, puisque la fréquence d'images chutait comme une pierre pendant l'animation, et je n'ai pas eu d'ombres de toute façon.CALayer Shadows disparaissant lors d'une animation UIView

Le code que j'utilise en ce moment est ci-dessous. Au départ, vous voyez une vue rouge, et lorsque vous appuyez dessus, la vue devient bleue. Le résultat final devrait être quelque chose de similaire à l'application de musique iPad; Quand un album est sélectionné, il se retourne pour révéler une vue sur le dos.

- (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]; 

} 

Répondre

12

Il se trouve que lorsque vous faites une animation UIView, il ignore la propriété de votre point de vue de clipsToBounds et des clips de toute façon (au moins pour les animations de type flip), donc si vous voulez que vos ombres soient toujours visibles , vous aurez besoin d'un cadre de vue assez grand qui contient tout.

+0

Pourriez-vous élaborer un peu? – sole007

+0

@ sole007: Cette réponse est probablement trop ancienne pour être pertinente aujourd'hui (et je n'ai pas eu de nouvelles connexions avec ce problème depuis), mais au cas où cela serait nécessaire, le problème était que même si clipsToBounds est défini à false, et vous avez utilisé CALayers pour rendre les ombres en dehors des limites d'une vue, en vous basant sur les clipsToBounds étant faux pour que ces ombres apparaissent, cela n'aurait aucune importance et ces ombres ne s'affichent pas. La solution consiste alors pour les limites d'affichage à contenir les ombres plutôt que les ombres se trouvent en dehors des limites. –

Questions connexes