如何参数化UIView动画?

发布于 2024-12-08 11:52:24 字数 941 浏览 0 评论 0原文

我有很多这样的 case 语句,它们会触发各种 UIView 动画。我想编写一个模块并对它们进行参数化,这样我就不必一遍又一遍地重复类似的代码。为animationRepeatCount 和animationRepeatCount 传递(参数化)值的最佳方法是什么?动画持续时间?

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;

I have lot of such case statements which would fire various UIView animations. I would like to write a module and parametrize these so I don't have to repeat similar code over and over. What is the best way to pass (parametrized) values for animationRepeatCount & 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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾`听者〃 2024-12-15 11:52:24

根据您的资源和动画的规律性,您可以创建一些实用方法来处理其中的大部分内容。

就像这样说:

+ (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)。它们也都基于相同的帧速率,因此我可以同时构建持续时间。

Depending on how regular your assets and animations are, you could make some utility methods to handle much of that.

Like say:

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

The above code loads up animations into an array with very regular naming conventions (XXXX.png). They are also all based on the same framerate so I can build the duration at the same time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文