2010-09-23 7 views
0

J'ai un UIView qui dans la méthode layoutSubviews repositionne ses sous-vues en fonction de l'orientation de l'iPad. Dans la méthode layoutSubviews, j'ai une CABasicAniamtion qui est censée animer le repositionnement des sous-vues. Les animations sont définies à une durée spécifique mais cette durée est ignorée et le repositionnement se produit immédiatement. Je sais que les animations se déclenchent car les méthodes AnimationDidStart et AnimationDidStop sont déclenchées. Je sais que cela a quelque chose à voir avec les CALayers de l'UIView mais je ne trouve rien sur le web pour expliquer comment résoudre ce problème. Toute aide serait appréciée.CABasicAnimation est ignoré pendant la rotation

if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
    { 
     NSLog(@"Orientation: Portrait"); 

     //Hide icon 

     CABasicAnimation *iconAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
     iconAnimation.fromValue = [NSValue valueWithCGPoint:[iconImageView center]]; 
     iconAnimation.toValue = [NSValue valueWithCGPoint:iconThinPosition]; 
     iconAnimation.duration = 2.7f; 
     iconAnimation.autoreverses = NO; 
     iconAnimation.repeatCount = 1; 
     iconAnimation.delegate = self; 
     [iconImageView.layer addAnimation:iconAnimation forKey:@"position"]; 
     //[iconImageView setCenter:iconThinPosition]; 

     [iconImageView.layer setPosition:iconThinPosition]; 
     //[iconImageView setTransform: CGAffineTransformIdentity]; 

     CABasicAnimation *textAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
     textAnimation.fromValue = [NSValue valueWithCGPoint:[textImageView center]]; 
     textAnimation.toValue = [NSValue valueWithCGPoint:textThinPosition]; 
     textAnimation.duration = 2.7f; 
     textAnimation.autoreverses = NO; 
     textAnimation.repeatCount = 1; 
     textAnimation.delegate = self; 
     [textImageView.layer addAnimation:textAnimation forKey:@"position"];   
     [textImageView.layer setPosition:textThinPosition]; 

    } 
    else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 
    { 
     NSLog(@"Orientation: Landscape");   

     // Show Icon 
     CABasicAnimation *iconAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
     iconAnimation.fromValue = [NSValue valueWithCGPoint:[iconImageView center]]; 
     iconAnimation.toValue = [NSValue valueWithCGPoint:iconShownPosition]; 
     iconAnimation.duration = 2.7f; 
     iconAnimation.autoreverses = NO; 
     iconAnimation.repeatCount = 1; 
     iconAnimation.delegate = self; 
     [iconImageView.layer addAnimation:iconAnimation forKey:@"position"]; 
     [iconImageView.layer setPosition:iconShownPosition]; 

     CABasicAnimation *textAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
     textAnimation.fromValue = [NSValue valueWithCGPoint:[textImageView center]]; 
     textAnimation.toValue = [NSValue valueWithCGPoint:textShownPosition]; 
     textAnimation.duration = 2.7f; 
     textAnimation.autoreverses = NO; 
     textAnimation.repeatCount = 1; 
     textAnimation.delegate = self; 
     [textImageView.layer addAnimation:textAnimation forKey:@"position"]; 
     [textImageView.layer setPosition:textShownPosition]; 
    } 
} 

Répondre

1

Je me demande si elle n'est pas ignorée, car il y a déjà une transaction d'animation en cours lorsque votre layoutSubviews est appelée. Une chose que vous pourriez essayer de confirmer est de surcharger -didRotateFromInterfaceOrientation et d'appeler votre layoutSubviews à partir de là. Voyez si vos vues s'animent alors.

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
// [self layoutSubviews]; 
    // Give it a second to settle after the rotation and then call 
    // layoutSuviews explicitly. 
    [self performSelector:@selector(layoutSubviews) withObject:nil afterDelay:1.0f]; 
} 

Une autre chose à penser est que, puisque vous n'animez la position de la couche du UIView, vous pouvez utiliser UIView animations au lieu de films d'animation. Quelque chose comme:

if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
    { 
     NSLog(@"Orientation: Portrait"); 

     //Hide icon 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:2.7f]; 
     [iconImageView setCenter:iconThinPosition]; 
     [textImageView setCenter:textThinPosition]; 
     [UIView commitAnimations]; 

    } 
    else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 
    { 
     NSLog(@"Orientation: Landscape");   

     // Show Icon 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:2.7f]; 
     [iconImageView setCenter:iconShownPosition]; 
     [textImageView setCenter:textShownPosition]; 
     [UIView commitAnimations]; 

    } 
Questions connexes