菜鸟教程小白 发表于 2022-12-13 09:51:37

ios - 带有同步计数线程的 UIView 动画


                                            <p>我正在尝试使用动画 block 来扩展 UIView,这非常有效。但是,我希望 UILabel 从 0 开始,每 0.01 秒加 1,直到达到 100。<br>我在动画之后创建了一个线程来完成这个任务,它可以工作,但它导致我设置的动画什么也不做。<br>我尝试了很多不同的事情,但没有运气。实现这一目标的最佳方法是什么?<br><br>我最简单的尝试与所有其他尝试的结果相同:<br><pre><code>[UIView animateWithDuration:1 animations:^{
    _lView.frame = CGRectMake(_lView.frame.origin.x,_lView.frame.origin.y+_lView.frame.size.height,_lView.frame.size.width,-500);
}];

[[initWithTarget:self selector:@selector(startcounting) object:nil]start];


-(void)startcounting{
for(int x=0; x&lt;100; x++){
    ;
    ++_mcount;
    dispatch_async(dispatch_get_main_queue(), ^{
      _cLabel.text = ;
    });
}
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p>有两个问题:<br><li>关于同时更改标签的帧动画,问题在于更改标签的文本会导致重新应用约束。正确的解决方案是不通过更改框架来制作动画,而是通过更改规定框架的约束然后调用 <code>layoutIfNeeded</code>在动画 block 中。见 <a href="https://stackoverflow.com/questions/14189362/animating-an-image-view-to-slide-upwards/14190042#14190042" rel="noreferrer noopener nofollow">Animating an image view to slide upwards</a> </li><li>关于标签的动画:<br><li>您无法保证可以如此快速地处理更新。 (事实上​​,您应该计划永远不会超过 60 fps。)</li><li>即使您能够充分降低更新频率,您也永远无法保证它们会以恒定速率进行处理(其他东西总是会阻塞主线程几毫秒,从而产生不一致的数字增量) . </li><br>因此,您应该改为使用计时器(或者更好的“显示链接”,它类似于计时器,但在屏幕准备好刷新时协调调用),计算耗时(这样计数器不会会受到其他事情的影响,但该值将从 0 快速变为 100 以产生您正在寻找的效果),并相应地更新标签。例如:<br><pre><code>@interface ViewController ()

@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic) CFTimeInterval startTime;

@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)startDisplayLink {
    self.displayLink = ;
    self.startTime = CACurrentMediaTime();
    forMode:NSRunLoopCommonModes];
}

- (void)stopDisplayLink {
    ;
    self.displayLink = nil;
}

- (void)handleDisplayLink:(CADisplayLink *)displayLink {
    CFTimeInterval elapsed = CACurrentMediaTime() - self.startTime;

    if (elapsed &lt; 1) {
      self.label.text = ;
    } else {
      self.label.text = @&#34;100&#34;;
      ;
    }
}
</code></pre> </li><br>因此,结合这两点,只需调整约束(在将导出添加到约束后)并调用 <code>startDisplayLink</code> (来自主线程)并且当 View 动画时,您的标签将在一秒钟内从 0 更新到 100:<br><pre><code>;
self.topConstraint.constant += self.lView.frame.size.height;
[UIView animateWithDuration:1.0 animations:^{
    ;
}];
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 带有同步计数线程的 UIView 动画,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32001755/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32001755/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 带有同步计数线程的 UIView 动画