为什么要使用UIView&UIImageView的动画不同吗? [英] Why do UIView & UIImageView animate differently?

查看:65
本文介绍了为什么要使用UIView&UIImageView的动画不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 4 buttons, all use the same 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 .

What amuses me is the different behavior of the UIview & UIImageView.

如果我使用延迟,则 UIImageView 实例仅按预期进行动画处理(从原点开始 ).但是,在不使用延迟块的情况下,UIImageView实例首先下降,然后对沿其原点进行动画设置().

The UIImageView instance only animates as intended (going up from origin) if I use delay. However, without using a delay block, UIImageView instance first drops down, then animates going up to its origin).

UIView 没有显示不同的行为.它从它的原点开始动画,然后上升.

The UIView doesn’t show different behaviors. It animates from its origin and goes up.

这是一个错误吗?

此问题与原始问题相切

推荐答案

无论如何切片,您所做的都是错误的.您不能同时添加视图并对其进行动画处理.您只能为界面中已经的视图设置动画.

What you're doing is wrong no matter how you slice it. You cannot add a view and animate it all in the same breath. You can only animate a view that is already in the interface.

这就是为什么在添加延迟时它可用于图像视图的原因:在进行动画处理时,图像视图已进入界面且界面已沉降.

That is why it works for the image view if you add the delay: by the time you animate, the image view has gotten into the interface and the interface has settled down.

它似乎适用于非imageview的事实真是愚蠢.

The fact that it seems to work for the non-imageview is just dumb luck.

这篇关于为什么要使用UIView&UIImageView的动画不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆