Freeze last frame of animation new code?

2019-05-11 19:06发布

问题:

I'm trying to freeze the last frame of an animation. The following code used to work, however after an update to iOS it no longer works. How can I write this code so that the last image stays on the screen?

animation.animationImages = [NSArray arrayWithObjects:

                             [UIImage imageNamed:@"watermelondrop2.png"],

                             [UIImage imageNamed:@"watermelondrop4.png"],

                             [UIImage imageNamed:@"watermelondrop6.png"],

                             [UIImage imageNamed:@"watermelondrop8.png"],

                             [UIImage imageNamed:@"watermelondrop10.png"],

                             [UIImage imageNamed:@"watermelondrop12.png"],

                             [UIImage imageNamed:@"watermelondrop14.png"],

                             [UIImage imageNamed:@"watermelondrop15.png"], nil];

    animation.animationDuration = .8;
    animation.animationRepeatCount = 1;
    [animation startAnimating];
    [self playwatermelondropsound];
    [self.view addSubview:animation];
    animation.image = [UIImage imageNamed:@"watermelondrop16.png"];

回答1:

I spend 40 my minutes to resolve your problem, and it was just so simple:

There are two way you can achieve what you want:

  1. Set the image you want on your screen as default image.
  2. or just do this,

    //make your last line to first line.  
    animation.image = [UIImage imageNamed:@"watermelondrop16.png"];
    animation.animationImages = [NSArray arrayWithObjects:
                         [UIImage imageNamed:@"watermelondrop2.png"],
                         [UIImage imageNamed:@"watermelondrop4.png"],
                         [UIImage imageNamed:@"watermelondrop6.png"],
                         [UIImage imageNamed:@"watermelondrop8.png"],
                         [UIImage imageNamed:@"watermelondrop10.png"],
                         [UIImage imageNamed:@"watermelondrop12.png"],
                         [UIImage imageNamed:@"watermelondrop14.png"],
                         [UIImage imageNamed:@"watermelondrop15.png"], nil];
    
    animation.animationDuration = .8;
    animation.animationRepeatCount = 1;
    [animation startAnimating];
    [self playwatermelondropsound];
    [self.view addSubview:animation];
    

And guess what it'll start working, as I have tested it. :)



回答2:

[self cleanStance];
NSArray *imageArray = [[NSArray alloc] initWithObjects:
                       [UIImage imageNamed:@"jump1.png"],
                       [UIImage imageNamed:@"jump2.png"],
                       nil];    
self.player.animationImages = imageArray;
self.player.animationDuration = 0.3;
//self.player.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview:self.player];
[self.player startAnimating];


- (void)cleanStance {
[self.player setImage:nil];
self.player.animationImages = nil;

}

This is the example to show the image jumping i thick this may help u



回答3:

Your code looks fine to me, except when I do it, I usually set the .image property of the UIImageView BEFORE I tell the UIImageView to start animating. Also, add [UIImage imageNamed:@"watermelondrop16.png"] to the end of your .animationImages array. Maybe that will fix your problem. Hope that Helps!