代码之家  ›  专栏  ›  技术社区  ›  typeoneerror

UIImageView动画,在最后一帧停止

  •  2
  • typeoneerror  · 技术社区  · 14 年前

    [self setAnimationRepeatCount:1];
    [self setAnimationDuration:1];
    [self startAnimating];
    // auto set the last image in the frame
    [self setImage:[UIImage imageNamed:[NSString stringWithFormat:@"frame-%d_29.png", self.frameSet]]];
    

    当动画完成时,它将显示图像。在OS4上没有这样的运气。我试过设置一个NSTimer并在它结束时设置静态帧,但是这种方法有时会出现明显的闪烁。有什么解决办法吗?

    1 回复  |  直到 14 年前
        1
  •  1
  •   typeoneerror    14 年前

    编辑

    比上一种解决方案更好的方法是简单地切换启动和设置的顺序:

    [self setImage:[UIImage imageNamed:[NSString stringWithFormat:@"frame-%d_29.png", self.frameSet]]];
    [self startAnimating];
    

    经过一番周折之后,我发现,如果将延迟设置为29/30秒(即~0.97秒),NSTimer解决方案实际上可以工作:

    - (void)doAnimation
    {
        [self setAnimationRepeatCount:1];
        [self setAnimationDuration:1];
        [self startAnimating];
        [NSTimer scheduledTimerWithTimeInterval:0.97f
                                         target:self
                                       selector:@selector(onTimer:)
                                       userInfo:nil
                                        repeats:NO];
    }
    
    - (void)onTimer:(NSTimer *)theTimer
    {
        [self stopAnimating];
        [self setImage:[UIImage imageNamed:[NSString stringWithFormat:@"frame-%d_29.png", self.frameSet]]];
    }