2009-07-19 114 views
1

我想讓導航欄比平常更慢地隱藏。更改動畫持續時間

我嘗試以下,但隱藏的時候,它消失的瞬間,而不是動畫出來(在視圖下方不正確動畫上):

[UIView beginAnimations:@"hideNavBar" context:nil]; 
[UIView setAnimationDuration:2.0]; 
[self.navigationController setNavigationBarHidden:value]; 
[UIView commitAnimations]; 

如果我替補:

[self.navigationController setNavigationBarHidden:value animated:YES]; 

然後使用通常的持續時間,而不是我的慢版本。 Hmmph。

我甚至試圖讓真正狡猾,做:

CGFloat *durationRef = &UINavigationControllerHideShowBarDuration; 
CGFloat oldDuration = *durationRef; 
*durationRef = 2.0; 
[self.navigationController setNavigationBarHidden:value animated:YES]; 
*durationRef = oldDuration; 

這就造成了EXE _的賦值BAD _訪問。有任何想法嗎?

回答

2

如果你想改變你需要實現自己的持續時間。 UINavigationBar是一個視圖,您可以抓住它的圖層並在沒有實際視圖的情況下移動它。基本上你做這樣的事情:

//This routine starts animating the layer of the navigation bar off screen 
- (void)hideNavigationBar { 
    CALayer *layer = self.navigationBar.layer; 

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
    animation.duration = 4.0; 
    animation.toValue = [NSNumber numberWithFloatValue:(layer.position.y - self.navigationBar.frame.size.height)]; 
    animation.delegate = self; 
    [touchedLayer addAnimation:animation forKey:@"slowHide"]; 
} 

//This is called when the animation completes. We have not yet actally 
//hidden the bar, so on redraw it will snap back into blace. We hide it 
//here before the redraw happens. 
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL) finished { 
    if (finished) { 
    [self.navigationController setNavigationBarHidden:YES animated:NO]; 
    } 
} 

動畫欄回來是類似的。請注意,當條形移動時,這不會在屏幕上縮放任何其他視圖,您將不得不在任何其他視圖需要調整時設置單獨的動畫。

改變速度是很多工作,UIKit沒有設置這樣做,並且圍繞蘋果內置的動畫工作就像走過地雷。除非你有一個非常有說服力的理由去做,否則我認爲你會發現讓所有事情正確行事的工作遠不止於此。

+0

最後,我決定reimplemebting UINavController是不值得的小增益優雅。但感謝您的答案。 – 2009-08-01 15:10:49

0

你仍然可以使用

[UIView beginAnimations:@"FadeOutNav" context:NULL]; 
[UIView setAnimationDuration:2.0]; 
self.navigationController.navigationBar.alpha=0.0; 
[UIView commitAnimations];