2010-09-23 78 views
0

我有一個UIView,它在layoutSubviews方法中根據iPad的方向重新定位其子視圖。在layoutSubviews方法中,我有一個CABasicAniamtion,它應該爲子視圖的重新定位創建動畫。動畫被設置爲特定的持續時間,但該持續時間被忽略,重新定位立即發生。我知道動畫正在觸發,因爲我看到了AnimationDidStart和AnimationDidStop方法被觸發。我知道這與UIView的CALayers有關,但我無法在網上找到任何解釋如何解決這個問題的方法。任何幫助,將不勝感激。旋轉過程中忽略CABasicAnimation

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

回答

1

我不知道它是否被忽略,因爲已經有一個動畫事務正在進行中,當你調用layoutSubviews時。你可以嘗試確認的一件事是重寫-didRotateFromInterfaceOrientation並從那裏調用你的layoutSubviews。看看你的觀點是否動畫。

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

另一個要考慮的是,因爲你只在動畫UIView的的層的位置,你可以使用UIView的動畫,而不是明確的動畫。例如:

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

    }