2

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

4

3 回答 3

7

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

[UIView performWithoutAnimation:^{
  [self.myButton setTitle:text forState:UIControlStateNormal];
  [self.myButton layoutIfNeeded];
}]; 
于 2015-08-25T22:57:06.043 回答
0

您的效果是否类似于下面的内容?我试图模仿您的情况,方法是在内部设置一个 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];
}
于 2015-08-25T23:21:39.190 回答
-1

禁用动画以供UIButton使用setAnimationsEnabled:(BOOL)enabled

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

于 2015-08-23T22:46:25.860 回答