Determine which of its animationImages a UIImageView is displaying?

只愿长相守 提交于 2019-12-18 06:57:19

问题


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?


回答1:


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;
}

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.



来源:https://stackoverflow.com/questions/17848438/determine-which-of-its-animationimages-a-uiimageview-is-displaying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!