为什么UIView& UIImageView动画有何不同?

时间:2017-02-10 21:09:20

标签: swift uiview uiimageview core-animation uiviewanimation

我有4个按钮,都使用相同的animate func

  • imageViewWithoutDelay
  • imageViewWithDelay
  • ViewWithoutDelay
  • ViewWithDelay

我的代码:

import UIKit

class ViewController: UIViewController {

    @IBAction func imageViewithoutDelay(_ sender: AnyObject) {
        let circleImage = UIImageView()
        // kindly add your own image.
        circleImage.image = UIImage(named: "empty")
        animate(inputView: circleImage, delay: false)
    }

    @IBAction func imageViewWithDelay(_ sender: UIButton) {

        let circleImage = UIImageView()
        circleImage.image = UIImage(named: "empty")
        animate(inputView: circleImage, delay: true)
    }

    func animate(inputView : UIView, delay: Bool){

        inputView.layer.cornerRadius = inputView.frame.size.width / 2
        inputView.clipsToBounds = true
        inputView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(inputView)

        let screenSize = UIScreen.main.bounds
        let screenHeight = screenSize.height * 0.10
        inputView.center.y = screenHeight
        view.setNeedsLayout()
        view.setNeedsDisplay()

        if delay == true{
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
            UIView.animate(withDuration: 2.5, animations: {
                inputView.alpha = 0.0
                let screenSize = UIScreen.main.bounds
                let screenHeight = screenSize.height * 0.10
                inputView.center.y -= screenHeight
            })
        })} else{
            UIView.animate(withDuration: 2.5, animations: {
                inputView.alpha = 0.0
                let screenSize = UIScreen.main.bounds
                let screenHeight = screenSize.height * 0.10
                inputView.center.y -= screenHeight
            })

        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

}

令我感到有趣的是UIview&的不同行为。 UIImageView

如果我使用延迟,则UIImageView实例仅按预期动画(从原点上升。但是,如果不使用延迟块,UIImageView实例首先会下降,然后动画上升到其原点

UIView没有显示不同的行为。它起源于动画并上升。

enter image description here 这是一个错误吗?

此问题与this原始问题相切。

1 个答案:

答案 0 :(得分:1)

无论你如何切片,你所做的都是错的。您无法同时添加视图并为其设置动画。您只能为已在界面中的视图设置动画。

这就是为什么它适用于图像视图的原因如果你添加了延迟:在你设置动画时,图像视图已经进入界面并且界面已经稳定下来。

它似乎适用于非imageview的事实只是运气不好。