【问题标题】:UIImageView transition between different images with animation带有动画的不同图像之间的 UIImageView 过渡
【发布时间】:2017-06-01 09:31:53
【问题描述】:

我有这段代码在同一张图片中重复淡入到视图控制器中

 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 淡入。

【问题讨论】:

    标签: ios swift xcode uiview uiimageview


    【解决方案1】:

    你是怎么做到的?

    您将需要:

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

      可变图像:[字符串] = []
      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!
    

    【讨论】:

    • 它成功了,非常感谢......这都是关于计时器的。
    • 感谢您不仅接受了答案,而且当它奏效时您微笑了。 :)
    猜你喜欢
    • 2012-03-03
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    相关资源
    最近更新 更多