UIView动画的发生速度比指定秒数快

时间:2016-08-01 12:31:13

标签: ios objective-c uiviewanimation

我有一个视图,图像中显示了一些文字。 我需要执行动画,在最后必须显示完整文本的字符。

我的界面中有一个徽标视图,因为我有动画视图。对于动画视图,前导,尾随,顶部和底部约束是基于superView(徽标视图)设置的。

我尝试过以下代码但是我指定的持续时间无法正常工作。

   - (void) animateLogo {
    [UIView animateWithDuration:10.0 animations:^{
        NSLog(@"animation started");
        if (self.animateViewLeadingConstraint.constant < 94) { //94 is the total width of the logo view
            self.animateViewLeadingConstraint.constant = self.animateViewLeadingConstraint.constant + 1;
        } else {
            self.animateViewLeadingConstraint.constant = 0;
        }
    } completion:^(BOOL finished) {
        NSLog(@"animation ended");
        [self.animateView layoutIfNeeded];
        [self animateLogo];
    }];
}

我已将日志附加了一段时间

2016-08-01 17:55:27.317 sample[20361:481531] animation started
2016-08-01 17:55:27.318 sample[20361:481531] animation ended
2016-08-01 17:55:27.318 sample[20361:481531] animation started
2016-08-01 17:55:27.318 sample[20361:481531] animation ended
2016-08-01 17:55:27.318 sample[20361:481531] animation started
2016-08-01 17:55:27.319 sample[20361:481531] animation ended
2016-08-01 17:55:27.319 sample[20361:481531] animation started
2016-08-01 17:55:27.319 sample[20361:481531] animation ended

我不确定我在上面的代码中犯了什么错误,或者我误解了UIView动画概念

任何帮助或提示都会非常感激。

2 个答案:

答案 0 :(得分:1)

请参阅此答案:How do I animate constraint changes?

您应该在动画视频的超级视图之前动画块以及layoutIfNeeded块本身中调用animations:。第一个调用确保应用任何布局更改。假设logoViewanimateView的超级视图:

- (void) animateLogo {
    self.animateViewLeadingConstraint.constant = 0.0;
    [self.logoView layoutIfNeeded];
    [UIView animateWithDuration:10.0 animations:^{
        NSLog(@"animation started");
        self.animateViewLeadingConstraint.constant = 94.0;
        [self.logoView layoutIfNeeded];
    } completion:^(BOOL finished) {
        NSLog(@"animation ended");
    }];
}

答案 1 :(得分:0)

最后,我得到了基于@stevekohls评论的解决方案

- (void) animateLogo {
    self.animateViewLeadingConstraint.constant = 0;
    [UIView animateWithDuration:5.0f animations:^{
        self.animateViewLeadingConstraint.constant = 94.0;
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5f animations:^{
            self.animateView.alpha = 0;
        } completion:^(BOOL finished) {
                self.animateViewLeadingConstraint.constant = 0;
                [self.view layoutIfNeeded];
                self.animateView.alpha = 1.0f;
                [self animateLogo];
        }];
    }];
}