Swift 中 UIImages 的淡入淡出动画 [英] Fade Animation for UIImages in Swift

查看:69
本文介绍了Swift 中 UIImages 的淡入淡出动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组图像,我希望在登录屏幕的背景中淡入淡出.我无法快速找到任何可以做到这一点的东西.有没有办法让我能够做到?

I have a set of images that i would like to have fade in the background of my login screen. I cant find anything in swift that can do this. Is there a way that i would be able to?

这是我当前的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    startAnimating()
    // Do any additional setup after loading the view, typically from a nib.
}

func startAnimating(){
    backgroundImage.animationImages = [
        UIImage(named: "background1.jpg"),
        UIImage(named: "background2.jpg")
    ]

    backgroundImage.animationDuration = 3
    backgroundImage.startAnimating()

}

推荐答案

以下代码将帮助您使用淡入淡出效果为图像设置动画并更改背景.

The below code will help you to animate image with fadeOut effect and change the background.

import UIKit

class ViewController: UIViewController {

    var imgView:UIImageView?  //class variable to display you bg image

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        imgView = UIImageView(frame: self.view.frame) //imgView to display background
        self.view.insertSubview(imgView, atIndex: 0)  //imgview add above the background of main view

        animateImage(1) /*call animageImage with parameter number as image number as i user image name as avatar1.png, avatar2.png, avatar3.png and avatar4.png*/

    }


    func animateImage(no:Int)
    {
        var imgNumber:Int = no
        let t:NSTimeInterval = 1;
        let t1:NSTimeInterval = 0;
        var name:String = "avatar\(imgNumber).png"
        imgView!.alpha = 0.4
        imgView!.image = UIImage(named:name);

        //code to animate bg with delay 2 and after completion it recursively calling animateImage method 
        UIView.animateWithDuration(2.0, delay: 0, options:UIViewAnimationOptions.CurveEaseOut, animations: {() in 
                                                      self.imgView!.alpha = 1.0;
                                                   }, 
                                         completion: {(Bool) in
                                                      imgNumber++;
                                                      if imgNumber>4  //only for 4 image
                                                      {
                                                          imgNumber = 1
                                                      }
                                                      self.animateImage(imgNumber); 
                                                   })
    }
}

这篇关于Swift 中 UIImages 的淡入淡出动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆