【问题标题】:Why do UIView & UIImageView animate differently?为什么 UIView 和 UIImageView 动画不同?
【发布时间】:2017-02-10 21:09:20
【问题描述】:

我有 4 个按钮,都使用相同的动画功能

  • imageViewWithoutDelay
  • imageViewWithDelay
  • 无延迟查看
  • 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()
    }

}

让我感到有趣的是UIviewUIImageView 的不同行为。

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

UIView 没有表现出不同的行为。它从原点开始动画并上升。

这是一个错误吗?

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

【问题讨论】:

    标签: swift uiview uiimageview core-animation uiviewanimation


    【解决方案1】:

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

    这就是为什么如果你添加延迟它适用于图像视图:当你制作动画时,图像视图已经进入界面并且界面已经稳定下来。

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

    【讨论】:

    • 我明白了。 1.到动画线的时候,是不是已经在界面中了? 2.为什么会这样?我的意思是为什么它会转移?不应该有为什么它不能发生以及为什么它可以用于 imageView 的原因。
    • 我不知道为什么它适用于非图像视图。 — 在 runloop 有机会循环之前,它不是“在界面中”。即使是零延迟也应该足够了,因为您将在下一个运行循环中回到主线程。在你的图像视图上试试看。
    • 你的意思是改成.seconds(0)?我这样做了,现在我的两个 imageViews 都下拉然后动画到原点。我也试过.nanoseconds(1) 仍然得到同样的错误结果。
    • 也许它会被优化掉。我在想用DispatchQueue.async {...} 而不是你的asyncAfter。或者使用我的delay utility 并尝试delay(0) — 但如果这不起作用,请尝试类似delay(.01)。让我们看看你发现了什么......
    猜你喜欢
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多