Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 设置UIButton的标题将取消动画_Objective C_Animation - Fatal编程技术网

Objective c 设置UIButton的标题将取消动画

Objective c 设置UIButton的标题将取消动画,objective-c,animation,Objective C,Animation,我不熟悉iOS动画,我的问题是: 在我们应用程序的登录屏幕上,有一个登录按钮。单击该按钮时,我们需要将其上移,然后更改其标题。向上移动它的动画如下所示: [UIView animateWithDuration:0.5 animations:^{ loginButton.frame = newLoginButtonFrame; }

我不熟悉iOS动画,我的问题是:

在我们应用程序的登录屏幕上,有一个登录按钮。单击该按钮时,我们需要将其上移,然后更改其标题。向上移动它的动画如下所示:

[UIView animateWithDuration:0.5
                 animations:^{
                                loginButton.frame = newLoginButtonFrame;
                            }
                 completion:^ (BOOL finished) {
                            }];
[UIView animateWithDuration:0.5
                 animations:^{
                                loginButton.frame = newLoginButtonFrame;
                            }
                 completion:^ (BOOL finished) {
                                [loginButton setTitle:@"Cancel" forState:UIControlStateNormal];
                            }];
viewBottomConstraint.constant = 32;
[UIView animateWithDuration:0.5
    animations:^{
        [self.view layoutIfNeeded]; // Called on parent view
    }];
}
这正如预期的那样有效

然后在我们尝试更改完成回调中的标题后,如下所示:

[UIView animateWithDuration:0.5
                 animations:^{
                                loginButton.frame = newLoginButtonFrame;
                            }
                 completion:^ (BOOL finished) {
                            }];
[UIView animateWithDuration:0.5
                 animations:^{
                                loginButton.frame = newLoginButtonFrame;
                            }
                 completion:^ (BOOL finished) {
                                [loginButton setTitle:@"Cancel" forState:UIControlStateNormal];
                            }];
viewBottomConstraint.constant = 32;
[UIView animateWithDuration:0.5
    animations:^{
        [self.view layoutIfNeeded]; // Called on parent view
    }];
}
奇怪的事情发生了:按钮跳回到原来的位置,好像动画被取消了


这是怎么回事?

如果使用自动布局,则应设置约束动画,而不是帧动画。例如,可以为底部约束设置一个出口,并设置如下动画:

[UIView animateWithDuration:0.5
                 animations:^{
                                loginButton.frame = newLoginButtonFrame;
                            }
                 completion:^ (BOOL finished) {
                            }];
[UIView animateWithDuration:0.5
                 animations:^{
                                loginButton.frame = newLoginButtonFrame;
                            }
                 completion:^ (BOOL finished) {
                                [loginButton setTitle:@"Cancel" forState:UIControlStateNormal];
                            }];
viewBottomConstraint.constant = 32;
[UIView animateWithDuration:0.5
    animations:^{
        [self.view layoutIfNeeded]; // Called on parent view
    }];
}

您可以在文档中找到更多信息

是否使用自动布局设置按钮?如果是的,你不应该为它设置动画是的,我正在使用自动布局-在这种情况下我应该如何设置动画?为什么设置标题会影响动画?谢谢