UIImageView在动画的不同图像之间转换

时间:2017-06-01 09:31:53

标签: ios swift xcode uiview uiimageview

我有这个代码,它会在同一张图片中重复淡入视图控制器

 override func viewDidAppear(_ animated: Bool) {
        let toImage = UIImage(named:"intro.png")
        UIView.transition(with: self.imageView,
                          duration:2,
                          options: [.repeat , .transitionCrossDissolve],
                          animations: { self.imageView.image = toImage},
                          completion: nil)


    }

现在我每次都要用不同的img逐渐淡入四张图像之间需要它。

1 个答案:

答案 0 :(得分:4)

你怎么能这样做?

您需要:

  1. 计时器
  2. 一个柜台
  3. 用于保存图像名称的数组

    var images:[String] = []
    var timer = Timer()
    var photoCount:Int = 0

  4. viewDidLoad 中,我为初始化做了这个。

    images = ["1","2","3"]
    imageView.image = UIImage.init(named: "1")
    timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(onTransition), userInfo: nil, repeats: true)
    

    这样做的神奇方法如下:

    func onTransition() {
        if (photoCount < images.count - 1){
            photoCount = photoCount  + 1;
        }else{
            photoCount = 0;
        }
    
        UIView.transition(with: self.imageView, duration: 2.0, options: .transitionCrossDissolve, animations: { 
            self.imageView.image = UIImage.init(named: self.images[self.photoCount])
        }, completion: nil)
    }
    

    不要忘记拉你的imageView参考插座。 :)

       @IBOutlet weak var imageView: UIImageView!