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

标签 ios objective-c uiviewanimation

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

我的界面中有一个 Logo View ,上面有一个动画 View 。对于动画 View ,前导、尾随、顶部和底部约束是基于 superView( Logo View )设置的。

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

   - (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 动画的概念

如有任何帮助或提示,我们将不胜感激。

最佳答案

查看此答案:How do I animate constraint changes?

您应该在动画 block 之前调用动画 View 的 super View 上的layoutIfNeeded,并在animations: block 本身中调用。第一次调用确保应用任何布局更改。假设 logoViewanimateView 的父 View :

- (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");
    }];
}

关于ios - UIView 动画发生的速度比指定的秒数快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38698985/

相关文章:

ios - Swift 3. 停止 UIView.animate

ios - 用户按下主页按钮后,UIButton 变为 Inanimate

ios - 在 swift 警报中使用哪个标识符代替 "present"

ios - tableview 连接到数据源和委托(delegate) - 如何

ios - Admob 钱少

ios - 在 Swift 中为 Parse 的 PFRelation 声明一个只读的@NSManaged 属性

objective-c - 将 ptrace 函数重命名为其他名称?如何?

ios - 为 subview 使用优化 UIScrollView

iPhone - 在应用程序处于后台时播放闹钟声音

objective-c - iOS 帧速率 - UIImage 始终为 30fps?