Determine which of its animationImages a UIImageVi

2019-01-26 21:53发布

问题:

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.