如何交叉分解xcode 9更改图像?

时间:2018-08-07 10:49:43

标签: ios swift xcode

我想以一种很好的方式更改背景图像,例如交叉溶解...

我设法让图像在3秒钟内改变。

您能帮我提供一个使代码以不错的方式更改的代码吗?

var timer = Timer()
var counter = 0

@IBOutlet weak var imageBG: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()

    repated()
}

func repated() {
    timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(ViewController.updatecounter), userInfo: nil, repeats: true)
}
@objc func updatecounter() {
counter += 1
 if counter == 14 {
    counter = 1
 }

 imageBG.image = UIImage(named: "\(counter).png")

}

1 个答案:

答案 0 :(得分:1)

upodatecounter中,您可以使用如下的UIView过渡:

@objc func updatecounter() {
  counter += 1
  if counter == 14 {
     counter = 1
  }

  UIView.transition(with: imageBG,
      duration:3.0,
      options: .transitionCrossDissolve,
      animations: { self.imageBG?.image = UIImage(named: "\(counter).png") },
      completion: nil)
}