Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

ios - Determine which of its animationImages a UIImageView is displaying?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...