【问题标题】:before completion of UIView animation executing code in completion block在 UIView 动画完成之前在完成块中执行代码
【发布时间】:2019-07-23 00:22:21
【问题描述】:

单击按钮时,我想将视图旋转 180 度。动画后我想隐藏和显示图像和标签。但是隐藏和显示图像和标签的完成代码在动画完成之前执行。检查下面的代码,让我知道我在哪里错了吗?

var animation = CABasicAnimation(keyPath: "transform.rotation.y")
    animation.fromValue = NSNumber(value: 0)
    animation.toValue = NSNumber(value: Double.pi)
    animation.repeatCount = 1
    animation.duration = 5.0

    UIView.animate(withDuration: 5.0, animations: {
        self.viewContainer.layer.add(animation, forKey: "rotation")
    }, completion: { finished in
        if finished {
            if self.strInfo == "Image" {
                self.strInfo = "Info"

                self.lblInfo.isHidden = false
                self.imageView.isHidden = true

                self.btnInfo.setBackgroundImage(UIImage(named:"close"), for: .normal)

            } else if self.strInfo == "Info"{
                self.strInfo = "Image"

                self.lblInfo.isHidden = true
                self.imageView.isHidden = false

                self.imageView.image = UIImage(named: self.strPhotoName)
                self.btnInfo.setBackgroundImage(UIImage(named:"info"), for: .normal)
            }
        }
    })

【问题讨论】:

    标签: ios swift uiviewanimation


    【解决方案1】:

    添加动画到图层没有等待时间你需要在动画块内部制作完整的动画逻辑,例如换帧或这样做

    self.viewContainer.layer.add(animation, forKey: "rotation")
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
    
        if self.strInfo == "Image" {
            self.strInfo = "Info"
    
            self.lblInfo.isHidden = false
            self.imageView.isHidden = true
    
            self.btnInfo.setBackgroundImage(UIImage(named:"close"), for: .normal)
    
        } else if self.strInfo == "Info"{
            self.strInfo = "Image"
    
            self.lblInfo.isHidden = true
            self.imageView.isHidden = false
    
            self.viewContainer.backgroundColor = .white
    
            self.imageView.image = UIImage(named: self.strPhotoName)
    
            self.btnInfo.setBackgroundImage(UIImage(named:"info"), for: .normal)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多