【问题标题】:Disable font animation in UIButton在 UIButton 中禁用字体动画
【发布时间】:2015-11-12 00:49:05
【问题描述】:

我正在为向下滑动的视图设置动画。在视图中有一个简单的 UIButton。视图和按钮具有固定的宽度和高度(我检查了添加颜色作为背景)。虽然使用时:

[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        //Slide Down the notification
        CGRect notificationsRect = self.notificationContainerView.bounds;
        notificationsRect.origin.y = 0;
        notificationViewController.view.frame = notificationsRect;
    } completion:^(BOOL finished) {
        [notificationViewController didMoveToParentViewController:self];
        self.currentNotification = notificationViewController; 
}];

然后视图完美地向下滑动,预计 UIButton 中的文本会淡入。它开始时非常小,并以正确的字体大小进行动画处理。

如何禁用 UIButton 中文本的动画?

【问题讨论】:

  • 我不明白“它开始时非常小并且动画到正确的字体大小。”。 “动画到正确的字体大小”?
  • 字体大小从 4pt 开始,然后动画到字体大小 14pt。但是按钮大小不会改变..
  • 你能详细介绍一下这个按钮吗?
  • 按钮只是一个UIButton,作为notificationContainerView的子视图。我什么也没做。
  • 作为一种解决方法,子类化按钮,并用[UIView performWithoutAnimations: ^ { 包装layoutSubviews 方法。让我知道这是否有帮助。

标签: ios objective-c uibutton uiviewanimation ios-animations


【解决方案1】:

【讨论】:

    【解决方案2】:

    使用performWithoutAnimation: 方法,然后强制布局立即发生,而不是稍后发生。

    [UIView performWithoutAnimation:^{
      [self.myButton setTitle:text forState:UIControlStateNormal];
      [self.myButton layoutIfNeeded];
    }]; 
    

    【讨论】:

    • 谢谢!很好,简单的答案。
    【解决方案3】:

    你的效果和下面的类似吗?我试图模仿您的情况,方法是在内部设置一个 UIButton 视图,并为按钮的标题设置一些文本。然后,我以类似于您的方式为按钮设置动画。正如您所看到的,在我正在滑动的视图中我的按钮文本上并没有真正发生淡入淡出,所以我想知道您是否还有其他事情在起作用。我有我在下面使用的代码来模拟这种情况。

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.myView = [[UIView alloc] initWithFrame:CGRectMake(200, 0, 100, 100)];
        self.myView.backgroundColor = [UIColor greenColor];
        UIButton *myButton= [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 80, 80)];
        myButton.backgroundColor = [UIColor blueColor];
        [myButton setTitle:@"fun times" forState:UIControlStateNormal];
        [self.myView addSubview:myButton];
        [self.view addSubview:self.myView];
    
        UIButton *resetButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 300, 50, 50)];
        resetButton.backgroundColor = [UIColor blackColor];
        [resetButton addTarget:self action:@selector(reset) forControlEvents:UIControlEventTouchUpInside];
    
        UIButton *goButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 100, 50, 50)];
        goButton.backgroundColor = [UIColor redColor];
        [goButton addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchUpInside];
    
        [self.view addSubview:goButton];
        [self.view addSubview:resetButton];
    }
    
    - (void)reset {
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            //Slide Up the notification
            CGRect notificationsRect = self.myView.bounds;
            notificationsRect.origin.y = 0;
            notificationsRect.origin.x = 200;
            self.myView.frame = notificationsRect;
        } completion:nil];
    }
    
    - (void)go {
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            //Slide Down the notification
            CGRect notificationsRect = self.myView.bounds;
            notificationsRect.origin.y = 200;
            notificationsRect.origin.x = 200;
            self.myView.frame = notificationsRect;
        } completion:nil];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      相关资源
      最近更新 更多