如何参数化UIView动画?

时间:2011-10-05 17:21:25

标签: objective-c animation

我有很多这样的案例陈述会触发各种UIView动画。我想写一个模块并参数化这些,所以我不必一遍又一遍地重复类似的代码。为animationRepeatCount和amp;传递(参数化)值的最佳方法是什么? animationDuration?

case 10: //Animation Number 10
    //.........
    imageAnimation2.animationImages = [NSArray arrayWithObjects:
                                      [UIImage imageNamed:@"monkey_dancing_01.png"],
                                      [UIImage imageNamed:@"monkey_dancing_02.png"],
                                      [UIImage imageNamed:@"monkey_dancing_03.png"],
                                      [UIImage imageNamed:@"monkey_dancing_04.png"],
                                      nil];
    imageAnimation2.animationRepeatCount = 4;
    imageAnimation2.animationDuration = 2;
    [imageAnimation2 startAnimating];
    [self loadSoundEffectAudio:@"Monkey_10"];
    [self performSelector:@selector(stopSoundEffectAudio) withObject:nil afterDelay:4];
    break;

1 个答案:

答案 0 :(得分:0)

根据您的资产和动画的规律程度,您可以使用一些实用程序方法来处理大部分内容。

喜欢说:

+ (void) loadAnimation:(UIImageView *)imageView name:(NSString *)filename
{
    NSMutableArray * array = [[NSMutableArray alloc] init];

    UIImage * image;
    int index = 1;
    NSString * string;

    do
    {
        string = [NSString stringWithFormat:@"%@%04d.png", filename, index++];
        image = [UIImage imageNamed:string];
        if(image)
            [array addObject:image];
    } while (image != nil);

    imageView.animationImages = array;
    imageView.animationDuration = [array count] / 18;
    [array release];
}

上面的代码将动画加载到具有非常规则命名约定(XXXX.png)的数组中。它们也都基于相同的帧速率,因此我可以同时构建持续时间。