停止最后一张图片的UIimageview动画

时间:2015-04-09 09:19:31

标签: ios objective-c uiimageview xcode5

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

4 个答案:

答案 0 :(得分:1)

首先将animationRepeatCount设置为1,并在启动动画后创建一个块,该块在此块中的动画持续时间后触发,将animationImageView图像设置为最后一个图像。

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

答案 1 :(得分:1)

如果您在开始动画之前知道animationRepeatCount

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

动态停止动画

animationImageView.animationRepeatCount = 0  //infinite loop

当你需要停止动画时

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

答案 2 :(得分:0)

您可以使用transitionWithView:duration:options:animation:completion: UIViewUIImageView设置动画。

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

你可以使用这个递归,并在完成博客中再次调用该方法并通过剩余的UIImages这种方式在最后UIImages停止不应该是一个问题。

我还建议您以这种方式从资源文件夹动态加载UIImages

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

始终写下UIImages的所有名称需要花费大量时间,并且可能会导致任何问题。这样你只需要订购你的图片,这样你就可以循环。

要使用此代码,请创建category NSMutableArray,然后您就可以使用它了!

答案 3 :(得分:0)

问题出在这条线上..

animationImageView.animationRepeatCount = 0;

通过设置animationRepeatCount = 0,默认设置为无限。 如果你想让它停在最后的图像,你可以使用

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