stop UIimageview animation at last image

我与影子孤独终老i 提交于 2019-12-12 18:24:39

问题


NSArray *imageNames = @[@"2_00000.png", @"2_00001.png", @"2_00002.png", @"2_00003.png",
                            @"2_00004.png", @"2_00005.png", @"2_00006.png", @"2_00007.png",
                            @"2_00008.png", @"2_00009.png", @"2_00010.png", @"2_00011.png",
                            @"2_00012.png", @"2_00013.png", @"2_00014.png", @"2_00015.png",
                            @"2_00016.png", @"2_00017.png", @"2_00018.png", @"2_00019.png",
                            @"2_00020.png", @"2_00021.png", @"2_00022.png", @"2_00023.png",
                            @"2_00024.png", @"2_00025.png", @"2_00026.png", @"2_00027.png",
                            @"2_00028.png", @"2_00029.png", @"2_00030.png", @"2_00031.png",
                            @"2_00032.png", @"2_00033.png", @"2_00034.png", @"2_00035.png",
                            @"2_00036.png", @"2_00037.png", @"2_00038.png", @"2_00039.png",
                            @"2_00040.png", @"2_00041.png", @"2_00042.png", @"2_00043.png",
                            @"2_00044.png", @"2_00045.png", @"2_00046.png", @"2_00047.png",
                            @"2_00048.png", @"2_00049.png", @"2_00050.png", @"2_00051.png",
                            @"2_00052.png", @"2_00053.png", @"2_00054.png", @"2_00055.png",
                            @"2_00056.png", @"2_00057.png", @"2_00058.png", @"2_00059.png",
                            @"2_00060.png", @"2_00061.png", @"2_00062.png", @"2_00063.png"];

NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 1; i < imageNames.count; i++) {
    [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}

// Normal Animation
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-6, 40, 200, 1034)];
animationImageView.animationImages = images;
animationImageView.animationDuration = 2;

animationImageView.animationRepeatCount = 0;




[self.view addSubview:animationImageView];



[animationImageView startAnimating];

回答1:


first set the animationRepeatCount to 1 and after starting the animation create a block that fires after the animation duration in this block set the animationImageView image to the last image .

dispatch_time_t dispatchAfter = dispatch_time(DISPATCH_TIME_NOW, animationImageView.animationDuration * NSEC_PER_SEC);
dispatch_after(dispatchAfter, dispatch_get_main_queue(), ^(void){

       animationImageView.image = [images lastObject];
});



回答2:


If you know the animationRepeatCount before starting your animation

animationImageView.animationRepeatCount = 1  //For 1 loop
animationImageView.image = animationImageView.animationImages.lastObject;

For stoping animation dynamically

animationImageView.animationRepeatCount = 0  //infinite loop

When you need to stop the animation

[animationImageView stopAnimating];
animationImageView.image = animationImageView.animationImages.lastObject;



回答3:


You could use transitionWithView:duration:options:animation:completion: of UIView for animating your UIImageView.

- (void)animatePictureTransition:(NSMutableArray *)arrayImages{

    [UIView transitionWithView:imageViewButton 
                      duration:1.0 
                       options:UIViewAnimationOptionTransitionFlipFromRight 
                    animations:^{

         UIImage *newImage = (UIImage *)arrayImages.firstObject;
         imageViewButton.image = newImage;

     } completion:^(BOOL finished){

         [arrayImages removeObjectAtIndex:0];
         if(arrayImages.count) {
             [self animatePicturesTransition:arrayImages];
         }
     }];
}

You could use this recursive, and call the method again inside the completion blog and pass through the remaining UIImages this way there shouldn't be a problem to stop at the last UIImages.

I would also recommend you, to load the UIImages dynamically from the resource folder in this way:

- (NSMutableArray *)getImagesWithSuffix:(NSString *)start andEndSuffix:(NSString *)end{

    NSMutableArray *imageArray = [[NSMutableArray alloc] init];

    for (int i = 1; i < 100; i++) {

        NSString *fileName = [NSString stringWithFormat:@"%@%d%@.png", start, i, end];
        if([self fileExistsInProject:fileName]){
            [imageArray addObject:[UIImage imageNamed:fileName]];

        } else {

            break;
        }
    }
    return imageArray;
}

Always writing all names of your UIImages takes a lot of time and can cause any problems. This way you just have to order your pictures so you can loop over.

To use this code, create a category of NSMutableArray and you can use it!




回答4:


problem is with this line..

animationImageView.animationRepeatCount = 0;

by setting animationRepeatCount = 0 its set to infinite by default. if u want it stop at last image you can go with

animationImageView.animationRepeatCount = 1; // specify no of times u need to run your animation.


来源:https://stackoverflow.com/questions/29534518/stop-uiimageview-animation-at-last-image

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