iphone – 逐渐平滑地停止UIImageView动画

栏目: IOS · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/14937287/stopping-uiimageview-animation-gradually-and-smoothly
我有以下简单的UI Image

View动画:

-(void) setupTheAnimation {
  self.imgView.animationImages = imagesArr;
  [self.imgView setAnimationRepeatCount:-1];
  self.imgView.animationDuration =0.9;
  [self.imgView startAnimating];
  [self performSelector:@selector(stopTheAnimation) withObject:nil afterDelay:4.0];
}

-(void) stopTheAnimation {
  [self.imgView stopAnimating];
}

但是当动画停止时我遇到了一个问题我不知道它停止的最后一帧是什么!所以动画的结尾根本不顺利.

所以我需要:

1)知道动画结束的最后一帧是什么,所以我把它设置为动画的最后一个图像,这样就可以顺利停止.

2)逐渐停止这个动画,即在它停止之前改变它的持续时间然后先将它减速然后停止它.

This is a link to the sample project .

我知道您的原始实现使用的是animationImages,但我不知道如何直接使用animationImages持续变化的持续时间.但是,这是一个非常简单的功能来实现自己.如果这样做,则可以在阵列中的图像之间编码动态持续时间值.

在下面的代码中,我用自定义步进函数替换animationImages,并在请求停止后动态调整持续时间.请注意,这与原始代码略有不同,后者指定了硬结束时间.此代码指定齿轮旋转应何时开始减速.

如果你真的有一个硬动画时段,你可以调整stopTheAnimation的调用以考虑你选择的减速因子(我只是在减速期间每步增加10%的持续时间,直到步数慢于给定的阈值) :

// my animation stops when the step duration reaches this value:
#define STOP_THRESHOLD_SECONDS 0.1f
#define NUM_IMAGES 35

@implementation ViewController
{
    NSMutableArray *imagesArr;
    int currentImage;
    BOOL stopRequested;
    NSTimeInterval duration;
}

-(void) setupTheAnimation {

    stopRequested = NO;
    currentImage = 0;
    duration = 0.9f / NUM_IMAGES;
    [self stepThroughImages];

    [self performSelector:@selector(stopTheAnimation) withObject:nil afterDelay:4.0];
}

- (void) stepThroughImages {

    self.imgView.image = [imagesArr objectAtIndex: currentImage];

    if (currentImage == NUM_IMAGES - 1) {
        currentImage = 0;
    } else {
        currentImage++;
    }

    if (stopRequested && duration < STOP_THRESHOLD_SECONDS) {
        // we're slowing down gradually
        duration *= 1.1f;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self stepThroughImages];
        });
    } else if (!stopRequested) {
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self stepThroughImages];
        });
    }
}

-(void) stopTheAnimation {
    stopRequested = YES;
}

翻译自:https://stackoverflow.com/questions/14937287/stopping-uiimageview-animation-gradually-and-smoothly


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Design in a Nutshell

Web Design in a Nutshell

Jennifer Niederst / O'Reilly Media, Inc. / 2006-02-21 / USD 34.99

Are you still designing web sites like it's 1999? If so, you're in for a surprise. Since the last edition of this book appeared five years ago, there has been a major climate change with regard to web......一起来看看 《Web Design in a Nutshell》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具