内容简介:翻译自: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)逐渐停止这个动画,即在它停止之前改变它的持续时间然后先将它减速然后停止它.
我知道您的原始实现使用的是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
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 优雅停止 SpringBoot 服务,拒绝 kill -9 暴力停止!
- 请停止结对编程
- 请停止学习框架!
- 请停止代码注释
- 驳 《驳 《驳 《停止学习框架》》》
- Netflix 宣布停止开发 Hystrix
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。