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

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

内容简介:翻译自: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


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

查看所有标签

猜你喜欢:

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

PHP程序设计

PHP程序设计

Kevin Tatroe、Rasmus Lerdorf / 邓云佳 / 中国电力出版社 / 2003-7-1 / 68.00

本书涵盖了创建一个高效PHP Web应用程序所需要的所有技术,其内容包括:PHP语言基础的详细信息,包括数据类型、变量、操作符和流控制语句。用专门章节讨论关于函数、字符串、数组和对象的基本内容。涵盖通用的PHP Web应用程序设计技术,如表单处理和验证、会话跟踪以及cookie。用和数据库无关的PEAR DB库与关系数据库(如MySQL和Oracle)进行交互的内容。介绍用PHP生成动态图像、创建一起来看看 《PHP程序设计》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试