【问题标题】:UIBarButtonItem: Change title font colour during animationUIBarButtonItem:在动画期间更改标题字体颜色
【发布时间】:2013-04-26 20:42:30
【问题描述】:

我已经使用自定义 UIButton 初始化了 UIBarButtonItem,并将其设置为导航控制器中的 rightBarButtonItem。

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

self.navigationItem.rightBarButtonItem = rightBarButtonItem;

通过 UIButton 的外观代理,我设置了 UIControlStates 的颜色(正常/禁用/突出显示)。 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
   ];

但是根本没有发生任何事情。我让它以某种方式工作的唯一方法是禁用/启用它。但是由于禁用和突出显示的颜色应该不同,我需要这两种状态,这不是一个真正的选择。

感谢任何帮助!

【问题讨论】:

  • 检查是否可以在动画块中将标题的alpha颜色从0更改为1。
  • 是的,我可以更改 Alpha 通道。 (Reinhard 还指出这个道具是可动画的)但这对我的变色问题并没有真正的帮助。我错过了什么吗?

标签: ios animation uibutton custom-controls uibarbuttonitem


【解决方案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 的属性。对不起,我退出了……

【讨论】:

  • 感谢您的回答。我明白了...我还可以使用某种“闪烁动画”(将字体颜色设置为突出显示颜色,然后恢复为正常颜色)您对如何更改 UIBarButtonItem 的字体颜色有任何提示吗?用自定义按钮初始化?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多