确定 UIImageView 显示的是哪个动画图像?

Determine which of its animationImages a UIImageView is displaying?(确定 UIImageView 显示的是哪个动画图像?)

本文介绍了确定 UIImageView 显示的是哪个动画图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找出 UIImage 在其动画的哪一帧上?当你启动它时,你给它一个动画图像数组,所以它显然必须将当前图像的索引存储在某个地方.你知道我怎么能找到这个吗?另外,有没有办法告诉 UIImageView 从哪一帧开始动画?

Is there a way to find out which frame of its animation a UIImage is on? When you start it you give it an array of animationImages, so it clearly has to be storing the index of the current image somewhere. Do you know how I can find this out? Also, is there a way to tell a UIImageView which frame to start its animation on?

推荐答案

在类转储 UIKit 后,我​​既找不到文档中的属性,也找不到可疑的 ivar 或私有方法,所以我能想到的最好的方法是:

I couldn't find neither a property in the documentation, nor a suspicious ivar or private method after class-dumping UIKit, so the best I could think of is:

NSDate *startDate; // instance variable

- (void)startAnimation
{
    startDate = [NSDate date];
    [imageView startAnimating];
}

- (int)currentImageIndex
{
    NSTimeInterval elapsed = -1 * [startDate timeIntervalSinceNow];
    int nFrames = imageView.animationImages.count;
    NSTimeInterval frameDuration = imageView.animationDuration / nFrames;
    int current = elapsed / frameDuration;
    return current % nFrames;
}

关于如何在特定图像上开始动画:使用 NSMutableArray 存储图像,并旋转数组,使其第一个元素是您要开始的图像,然后重新- 设置图像视图的 animationImages 属性.要旋转 NSMutableArray,请参阅 这个问题.

As to how to start the animation at a particular image: use an NSMutableArray for storing the images, and rotate the array so that its first element is the image you want to begin with, then re-set the animationImages property of the image view. To rotate an NSMutableArray, see the answers to this question.

这篇关于确定 UIImageView 显示的是哪个动画图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:确定 UIImageView 显示的是哪个动画图像?

基础教程推荐