2013-05-02 66 views
1

我已經使用自定義UIButton初始化UIBarButtonItem,並將其設置爲導航控制器中的rightBarButtonItem。UIBarButtonItem:在動畫過程中更改標題字體顏色

UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightNavigationButton];//rightNavigationButton is the custom UIButton I set up before 

self.navigationItem.rightBarButtonItem = rightBarButtonItem; 

通過UIButton的外觀代理,我設置了UIControlStates(Normal/Disabled/Highlighted)的顏色。 UIBarButtonItem的行爲如預期。

當按下「保存按鈕」時,我想突出顯示UIBarButtonItem,以提供已保存內容的視覺反饋。 我想要做的是通過在保存動畫期間改變標題的顏色來模擬UIBarButtonItem的突出顯示,因爲它似乎不能以編程方式觸發突出顯示動畫(或設置選定的屬性)(also mentioned in this post

所以我所做的就是設置一個IBOutlet屬性,將它連接到InterfaceBuilder中的UIBarButtonItem併爲其分配rightBarButtonItem。當按下「保存按鈕」時,我試着這樣:

[UIView animateWithDuration:0.5f 
         delay:0.0f 
        options:UIViewAnimationCurveEaseOut 
       animations:^{ 
          //some other animation code here 
          [self.barButtonItemOutlet setTitleTextAttributes:[NSDictionary   dictionaryWithObjectsAndKeys: 
          [UIFont fontWithName:FONT_ICON size:FONTSIZE],UITextAttributeFont, 
          HIGHLIGHT_COLOUR,UITextAttributeTextColor, 
          [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],UITextAttributeTextShadowOffset, 
          [UIColor colorWithRed:0 green:0 blue:0 alpha:0.0],UITextAttributeTextShadowColor,nil] 
          forState:UIControlStateNormal]; 

       } 
       completion:nil 
    ]; 

但是根本沒有什麼事情發生。我以某種方式工作的唯一方法是禁用/啓用它。但由於着色禁用和突出顯示應該是不同的,我需要這兩種狀態,這不是一個真正的選擇。

任何幫助表示讚賞!

+0

檢查是否可以將動畫塊中標題的字母顏色從0改爲1。 – sridevi 2013-05-02 16:36:43

+0

是的,我可以改變alpha通道。 (Reinhard也指出這個道具是可以動畫的)但是這並不能真正幫助我改變顏色的問題。我錯過了什麼嗎? – codingPanda 2013-05-03 09:31:38

回答

1

好,UIView類的docs說:UIView類的屬性如下動畫:

@property frame 
    @property bounds 
    @property center 
    @property transform 
    @property alpha 
    @property backgroundColor 
    @property contentStretch 

所以顏色不能動畫,對不起。

編輯:我的回答有關UIView動畫,但也有CALayer動畫更靈活。

我找到this link,也許你也可以動畫的顏色,如果你使用CALayer動畫。讓我看看它。
編輯: 那麼我發現核心動畫this very useful book chapter。不幸的是,它似乎沒有解決您的問題,因爲UIBarButtonItem的標題顏色似乎不是可以動畫的CALayer的屬性。對不起,我退出...

+0

感謝您的回答。我看到...我也可以用某種「眨眼動畫」(設置字體顏色來突出顯示顏色,然後恢復到普通顏色)您是否有任何提示,瞭解如何更改UIBarButtonItem的字體顏色用自定義按鈕初始化? – codingPanda 2013-05-03 09:15:33