我有这个代码,它会在同一张图片中重复淡入视图控制器
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逐渐淡入四张图像之间需要它。
答案 0 :(得分:4)
你怎么能这样做?
您需要:
用于保存图像名称的数组
var images:[String] = []
var timer = Timer()
var photoCount:Int = 0
在 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!