【问题标题】:Is it possible to animate an UIImage?是否可以为 UIImage 设置动画?
【发布时间】:2012-01-22 15:04:53
【问题描述】:

我想知道是否有动画UIImage

我知道UIImageViews 可以制作动画,但有没有办法直接在UIImage 中制作动画。

也许使用 Open GL ES 或其他什么?

或者还有其他方法可以为MPMediaItemArtwork 设置动画吗?

提前致谢!

【问题讨论】:

  • 你在找什么样的动画?简单的平移和移动?还是换显示器?
  • 基本上在多帧之间切换,有点像.gif。就像先显示 image1 然后显示 image2 然后显示 image3 等等。

标签: iphone objective-c xcode ios5 uiimage


【解决方案1】:

Swift 4 版本:

let animationImages = [UIImage(named: "image1.png"), UIImage(named: "image2.png")]
imageView.animationImages = animationImages
imageView.animationDuration = 10
imageView.startAnimating()

【讨论】:

    【解决方案2】:

    检查 UIImage 的 +animatedImageWithImages:duration:+animatedImageNamed:duration:

    来自UIImage Class Reference

    从现有图像集创建并返回动画图像。

    此方法通过将一系列数字附加到 name 参数中提供的基本文件名来加载一系列文件。例如,如果 name 参数的内容为“image”,则此方法将尝试从名称为“image0”、“image1”等一直到“image1024”的文件加载图像。动画图像中包含的所有图像都应具有相同的大小和比例。

    【讨论】:

      【解决方案3】:

      如果您的意思是带有几张图片的动画,请使用以下代码:

         // create the view that will execute our animation
       UIImageView* YourImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
      
       // load all the frames of our animation
       YourImageView.animationImages = [NSArray arrayWithObjects:    
                                   [UIImage imageNamed:@"01.gif"],
                                   [UIImage imageNamed:@"02.gif"],
                                   [UIImage imageNamed:@"03.gif"],
                                   [UIImage imageNamed:@"04.gif"],
                                   [UIImage imageNamed:@"05.gif"],
                                   [UIImage imageNamed:@"06.gif"],
                                   [UIImage imageNamed:@"07.gif"],
                                   [UIImage imageNamed:@"08.gif"],
                                   [UIImage imageNamed:@"09.gif"],
                                   [UIImage imageNamed:@"10.gif"],
                                   [UIImage imageNamed:@"11.gif"],
                                   [UIImage imageNamed:@"12.gif"],
                                   [UIImage imageNamed:@"13.gif"],
                                   [UIImage imageNamed:@"14.gif"],
                                   [UIImage imageNamed:@"15.gif"],
                                   [UIImage imageNamed:@"16.gif"],
                                   [UIImage imageNamed:@"17.gif"], nil];
      
       // all frames will execute in 1.75 seconds
       YourImageView.animationDuration = 1.75;
       // repeat the animation forever
       YourImageView.animationRepeatCount = 0;
       // start animating
       [YourImageView startAnimating];
       // add the animation view to the main window 
       [self.view addSubview:YourImageView];
      

      Source

      【讨论】:

        【解决方案4】:

        你可以用这个。

        arrayOfImages=[[NSMutableArray alloc]init];
        for(int i=1;i<=54;i++)
        {
        NSString *nameResources=@"haKsequence.png";
        NSString *changedString= [NSString stringWithFormat:@"%d", i];
        NSString *stringWithoutSpaces = [nameResources     stringByReplacingOccurrencesOfString:@"K" withString:changedString];
        [arrayOfImages addObject:(id)[UIImage imageNamed:stringWithoutSpaces].CGImage];
        }
        
        
        self.view.userInteractionEnabled=NO;
        i1.hidden=YES;
        image1=[[UIImageView alloc]init];
        image1.backgroundColor=[UIColor clearColor];
        image1.frame = button.frame;//i1 is obshaped buttion
        [self.view addSubview:image1];
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
        animation.calculationMode = kCAAnimationDiscrete;
        animation.duration = 1;
        animation.values   = arrayOfMonkeyImages;
        [animation setDelegate:self];
        animation.repeatCount=3;
        [animation setRemovedOnCompletion:YES];
        [image1.layer addAnimation:animation forKey:@"animation"];
        

        【讨论】:

          【解决方案5】:

          创建一个UIImageView并将animationImages的属性设置为UIImages的数组

          这是一个例子:

          NSArray *animationFrames = [NSArray arrayWithObjects:
            [UIImage imageWithName:@"image1.png"],
            [UIImage imageWithName:@"image2.png"], 
            nil];
          
          UIImageView *animatedImageView = [[UIImageView alloc] init];
          animatedImageView.animationImages = animationsFrame;
          [animatedImageView startAnimating];
          

          如果您的目标是 iOS 5,您可以直接在 UIImage 中执行此操作,而无需使用 UIImageView

              +(UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration 
          

          例如,

              [UIImage animatedImagesWithImages:animationFrames duration:10];
          

          【讨论】:

            猜你喜欢
            • 2015-10-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-05
            相关资源
            最近更新 更多