斯威夫特 - 如何动画图像?

Che*_*ile 18 ios swift

我试图在特定的持续时间内动画图像.它在Objective C中运行正常.但是,它不适用于Swift,我的错误在哪里?

Objective-C的代码是 -

-(void)viewDidLoad
{
   [super viewDidLoad];
   NSMutableArray *imgListArray=[NSMutableArray array];
   for (int i=0; i<=11; i++)

   {
       NSString *strImageName=[NSString stringWithFormat:@"c%d.png", i];
       NSLog(@"%@",strImageName);
       UIImage *image=[UIImage imageNamed:strImageName];
       [imgListArray addObject:image];
   }

   self.imgView.animationImages = imgListArray;
   self.imgView.animationDuration =1.0f;    
   [self.imgView startAnimating];
   // Do any additional setup after loading the view, typically from a nib
}
Run Code Online (Sandbox Code Playgroud)

迅捷守则是 -

override func viewDidLoad()
{
   super.viewDidLoad()

   var imgListArray :NSMutableArray = []
   for countValue in 1...11
   {
      var strImageName : String = "c\(countValue).png"
      var image = UIImage(named:strImageName) // suggested by Anil
      imgListArray.addObject(image)
   }

   // Swift code HERE for Objective c
}
Run Code Online (Sandbox Code Playgroud)

Ani*_*ese 29

[UIImage imageNamed (strImageName)]
Run Code Online (Sandbox Code Playgroud)

这不是快速的代码.很快就会

UIImage(named:strImageName)  
Run Code Online (Sandbox Code Playgroud)

修改后的代码

var imgListArray :NSMutableArray = []
for countValue in 1...11
    {

        var strImageName : String = "c\(countValue).png"
        var image  = UIImage(named:strImageName)
        imgListArray .addObject(image)
    }

    self.imageView.animationImages = imgListArray;
    self.imageView.animationDuration = 1.0
    self.imageView.startAnimating()
Run Code Online (Sandbox Code Playgroud)


Jef*_*Neo 21

对于Swift 2,请[UIImage]改用.

var images: [UIImage] = []
for i in 1...2 {
    images.append(UIImage(named: "c\(i)")!)
}
myImageView.animationImages = images
myImageView.animationDuration = 1.0
myImageView.startAnimating()
Run Code Online (Sandbox Code Playgroud)


Tia*_*ida 15

在swift中你可以更进一步,并有一个简单的行来做到这一点:

let loadingImages = (1...11).map { UIImage(named: "c\($0)")! }
Run Code Online (Sandbox Code Playgroud)

然后你可以完成剩下的工作并将其注入imageView

self.imageView.animationImages = loadingImages
self.imageView.animationDuration = 1.0
self.imageView.startAnimating()
Run Code Online (Sandbox Code Playgroud)


Mil*_*hah 10

在swift 3中 - 创建图像阵列并仅为其制作动画.

func animate_images()
{
    let myimgArr = ["1.jpg","2.jpg","3.jpg"]
    var images = [UIImage]()

    for i in 0..<myimgArr.count
    {
        images.append(UIImage(named: myimgArr[i])!)
    }

    imgView_ref.animationImages = images
    imgView_ref.animationDuration = 0.04
    imgView_ref.animationRepeatCount = 2
    imgView_ref.startAnimating()
}
Run Code Online (Sandbox Code Playgroud)

而对于停止动画只需写

 imgView_ref.stopAnimating()
Run Code Online (Sandbox Code Playgroud)


win*_*rrr 5

斯威夫特 3+

UIImageViews可以用两种不同的方式对图像进行动画处理(其他答案已经深入介绍了第一种方式):

    • 创建一个[UIImage]包含要制作动画的图像的数组
    • 使用此数组设置animationImages视图的属性
    • 设置animationDuration属性
    • 开始动画

以下代码使用#imageLiterals

let images = [img1, img2, img3] // use of #imageLiterals here
let animation = UIImage.animatedImage(with: images, duration: 1)
self.myImageView.image = animation
Run Code Online (Sandbox Code Playgroud)

专业人士:

如果您必须多次更改 UIImageView 的图像,并且可能其中一张图像应该是动画的,那么您不必再每次都启动/停止动画,也不需要将animatedImages 属性设置回以nil显示图像存储在图像视图的图像属性中。

缺点:

UIImage如果动画封装在单个而不是数组中,则无法启动/停止动画。


更多关于#imageLiterals

如果您想使用图像文字,请键入imageLiteral或仅键入资产文件夹中的图像名称,Xcode 的代码完成将会建议它。

进一步阅读:

  • 另一篇关于使用动画的好博客文章#imageLiterals#colorLiterals