2014-07-03 34 views
12

在我的應用程序中,我有一個頁面視圖控制器,允許用戶在應用程序的不同「部分」之間滑動,並在導航欄頂部將標題文本更改爲新部分用戶已通過pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:刷卡。目前它在動畫完成時立即更改標題文本。我想用一些動畫來改善它,一種微妙的淡入淡出效果。動畫導航欄標題文本更改

我第一次試圖實現[UIView animationWithDuration:...]來改變標題文字的動畫效果,但它不動畫,並且仍然立即更新。

然後我想知道是否有可能更新導航欄標題的alpha值,因爲用戶會根據滾動的距離來水平滾動,當下一部分即將出現在屏幕上時達到0 alpha,那麼我可以在文本爲0時立即更改文本,然後快速淡入1 alpha。但是我沒有看到UIPageViewControllerDelegate上滾動位置更新時調用的方法。

如果可能,而不是淡入淡出,我可以淡入淡出以及移動導航欄中的標題文本位置,就像從通過滑動手勢從推入式導航返回時出現的默認動畫一樣。當用戶滾動並在另一側提供下一個部分標題時,我會滑動舊部分標題,以便在轉換完成時,前一部分標題脫離屏幕,並且新標題完全居中以便更換完成。但是,這又需要確切知道用​​戶滾動頁面視圖控制器的程度。

是否有可能實現任何期望的動畫?

+0

您是否嘗試過爲自己的導航標題使用自定義視圖? self.navigationItem.titleView = customNavTitleLabel; – Zhang

+0

@張我沒有,我會調查。有沒有辦法從現有標題中獲得'UILabel',這樣我就可以獲取默認的標題(它會隨着粗體文本的啓用而變化),改變它,並將其設置爲已調整的標題? – Joey

+0

我想我已經看到人們使用一些瘋狂的遞歸循環來找到導航標題子視圖:P你可以嘗試,如果你想。 – Zhang

回答

36

如果你想不同的標題字符串之間的動畫,使用以下命令:

CATransition *fadeTextAnimation = [CATransition animation]; 
fadeTextAnimation.duration = 0.5; 
fadeTextAnimation.type = kCATransitionFade; 

[self.navigationController.navigationBar.layer addAnimation: fadeTextAnimation forKey: @"fadeText"]; 
self.navigationItem.title = "My new title"; 

您可以調整時間和設置,以適應定時功能,當然。

也有其他可能在不同的環境下工作類型的動畫(感謝@inorganik):

kCATransitionFade 
kCATransitionMoveIn 
kCATransitionPush 
kCATransitionReveal 
+1

喜歡這個解決方案!拿起我的袖子的大把戲。 – inorganik

+0

如果我的經驗是確定的,那麼你需要通過[project]> [target]> Build Phases> Link Binaries> QuartzCore.framework來添加QuartzCore框架。 –

2

解決方案是使用頁面視圖控制器隱藏的scrollView的委託方法創建自定義標題併爲其位置設置動畫。正如張春賢說,自定義標題簡直是self.navigationItem.titleView = customNavTitleLabel;

0

找到最好的辦法就是這一類:

@implementation UIViewController (ControllerNavigationEffects) 

-(void) setNavigationTitleWithAnimation:(NSString *) title { 
    if ([self.navigationItem.title isEqualToString:title]) { 
     return; 
    } 
    @weakify(self); 
    float duration = 0.2; 
    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
     @strongify(self); 
     self.navigationItem.titleView.alpha = 0; 
    } completion:^(BOOL finished) {}]; 

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
     @strongify(self); 
     self.navigationItem.titleView = nil; 
     self.navigationItem.title = title; 

     [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
      self.navigationItem.titleView.alpha = 1; 
     } completion:nil]; 
    }); 
} 

-(void) setNavigationTitleViewWithAnimation:(UIView *) titleView { 
    if ([self.navigationItem.titleView isKindOfClass:[titleView class]]) { 
     return; 
    } 
    @weakify(self); 
    float duration = 0.2; 

    CATransition *fadeTextAnimation = [CATransition animation]; 
    fadeTextAnimation.duration = duration; 
    fadeTextAnimation.type = kCATransitionFade; 

    [self.navigationController.navigationBar.layer addAnimation: fadeTextAnimation forKey: @"fadeText"]; 
    self.navigationItem.title = @""; 


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
     @strongify(self); 
     self.navigationItem.title = @""; 
     self.navigationItem.titleView = titleView; 

     [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
      self.navigationItem.titleView.alpha = 1; 
     } completion:nil]; 

    }); 
} 
27

爲了方便,阿什利米爾斯在Swift中的解決方案:

11/16/2016更新爲Swift 3(感謝n13)

let fadeTextAnimation = CATransition() 
fadeTextAnimation.duration = 0.5 
fadeTextAnimation.type = kCATransitionFade 

navigationController?.navigationBar.layer.add(fadeTextAnimat‌​ion, forKey: "fadeText") 
navigationItem.title = "test 123" 

斯威夫特2.x的

let fadeTextAnimation = CATransition() 
fadeTextAnimation.duration = 0.5 
fadeTextAnimation.type = kCATransitionFade 

navigationController?.navigationBar.layer.addAnimation(fadeTextAnimation, forKey: "fadeText") 
navigationItem.title = "test 123" 

我小費我的帽子,阿什利!

+1

Swift 3 navigationController?.navigationBar.layer.add(fadeTextAnimation,forKey:「fadeText」) – n13

+0

@ n13感謝提醒:) –

+1

注意到'type'屬性的文檔。 「過渡的名稱,當前合法的過渡類型包括淡入淡出,移入,推入和顯示,默認爲淡出」,所以'type'屬性如果開始淡出是不必要的。 –