在 UIButton 中禁用字体动画

Disable font animation in UIButton

我正在为向下滑动的视图制作动画。在视图中有一个简单的 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 中的文本动画?

使用 setAnimationsEnabled:(BOOL)enabled

禁用 UIButton 的动画

参考:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/clm/UIView/setAnimationsEnabled

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

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

你的效果和下面的类似吗?我试图通过在内部使用 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];
}