0

我有一个按钮,我想给它一些动画。一切正常,但我试图找出一种方法来反转动画的图像序列顺序,以便按钮似乎恢复它的先前状态。我在文档中找不到任何内容,似乎无法反转图像序列?如果无法颠倒顺序,我怎样才能给按钮一个不同的图像序列?我应该用一个新的阵列把它收起来吗?

代码:

 animatedButton = [[UIImageView alloc] initWithFrame:animButtonRect];
[animatedButton setImage:[UIImage imageNamed:@"messaging_00014.png"]];
[animatedButton setUserInteractionEnabled:YES];

animatedButton.animationImages = [NSArray arrayWithObjects:
                            [UIImage imageNamed:@"mov_00000.png"],
                            [UIImage imageNamed:@"mov_00001.png"],
                            [UIImage imageNamed:@"mov_00002.png"],
                            [UIImage imageNamed:@"mov_00003.png"],
                            [UIImage imageNamed:@"mov_00004.png"],
                            [UIImage imageNamed:@"mov_00005.png"],
                            [UIImage imageNamed:@"mov_00006.png"],
                            [UIImage imageNamed:@"mov_00007.png"],
                            [UIImage imageNamed:@"mov_00008.png"],
                            [UIImage imageNamed:@"mov_00009.png"],
                            [UIImage imageNamed:@"mov_00010.png"],
                            [UIImage imageNamed:@"mov_00011.png"],
                            [UIImage imageNamed:@"mov_00012.png"],
                            [UIImage imageNamed:@"mov_00013.png"],
                            [UIImage imageNamed:@"mov_00014.png"],
 nil];

 animatedButton.animationDuration = 1.0;
 animatedButton.animationRepeatCount = 1;
[self.view addSubview:animatedButton];
4

2 回答 2

0

I know this reply is a little late but I have been working on reverse animations and thought Id share a nice little piece of code.

Note: This code is for an animation sequence of 30 images with the image names being 4 digits long. eg. image_0001.jpg

-(void)animation
{
 NSMutableArray *animationArray = [[NSMutableArray alloc] init];

    int animationSizeTwo = 30;

    for (int i=0; i< animationSizeTwo; i++) {

        NSString *zeros = @"000";

        if ( i >= 1000 ) {

            zeros = @"";

        }
        else if ( i >= 100 ) {

            zeros = @"0";

        }
        else if ( i >= 10 ) {

            zeros = @"00";
        }
        NSString *animationNameTwo = [NSString stringWithFormat:@"(name)"];

        NSString *imageName = [NSString stringWithFormat:@"%@%@%d.jpg", animationNameTwo, zeros, i];

        [animationArrayTwo addObject:[UIImage imageNamed:imageName]];


    }


    _imageView.animationImages = animationArrayTwo;
    _imageView.animationDuration = 1;
    _imageView.animationRepeatCount = 1;
    _imageView.image = [_imageView.animationImages lastObject];
    [_imageView startAnimating];
}

Then to reverse the animation your simply use this bit of code inplace of the image count:

int animationSizeTwo = 0;

    for (int i=30; i> animationSizeTwo; i--) {

As I said it might be too late to answer your question but could always come in handy in the future, Best of luck

于 2013-02-05T02:21:04.447 回答
0

我知道这是一个老问题,但由于这里从来没有回答过,所以这是一个解决方案。只需将相同的图像反向放置并跳过序列中的第一个和最后一个。

animatedButton.animationImages = [NSArray arrayWithObjects:
                            [UIImage imageNamed:@"mov_00000.png"],
                            [UIImage imageNamed:@"mov_00001.png"],
                            [UIImage imageNamed:@"mov_00002.png"],
                            [UIImage imageNamed:@"mov_00003.png"],
                            [UIImage imageNamed:@"mov_00004.png"],
                            [UIImage imageNamed:@"mov_00005.png"],
                            [UIImage imageNamed:@"mov_00006.png"],
                            [UIImage imageNamed:@"mov_00007.png"],
                            [UIImage imageNamed:@"mov_00008.png"],
                            [UIImage imageNamed:@"mov_00009.png"],
                            [UIImage imageNamed:@"mov_00010.png"],
                            [UIImage imageNamed:@"mov_00011.png"],
                            [UIImage imageNamed:@"mov_00012.png"],
                            [UIImage imageNamed:@"mov_00013.png"],
                            [UIImage imageNamed:@"mov_00014.png"],
                            [UIImage imageNamed:@"mov_00013.png"],
                            [UIImage imageNamed:@"mov_00012.png"],
                            [UIImage imageNamed:@"mov_00011.png"],
                            [UIImage imageNamed:@"mov_00010.png"],
                            [UIImage imageNamed:@"mov_00009.png"],
                            [UIImage imageNamed:@"mov_00008.png"],
                            [UIImage imageNamed:@"mov_00007.png"],
                            [UIImage imageNamed:@"mov_00006.png"],
                            [UIImage imageNamed:@"mov_00005.png"],
                            [UIImage imageNamed:@"mov_00004.png"],
                            [UIImage imageNamed:@"mov_00003.png"],
                            [UIImage imageNamed:@"mov_00002.png"],
                            [UIImage imageNamed:@"mov_00001.png"],
 nil];
于 2020-01-20T08:04:33.490 回答