当前位置: 首页 > 知识库问答 >
问题:

为什么我的UIView动画在添加了子视图之后就不工作了?

钱和平
2023-03-14

我正在做一个从屏幕上方滑出的祝酒词,如下所示:

但是当我添加uilabel作为toast容器的子视图时,toast将不再动画和显示。下面是我的showtoast()函数:

func showToast(message: String) {
    let screenSize = UIScreen.main.bounds.size
    let containerView = UIView()
    containerView.translatesAutoresizingMaskIntoConstraints = false
    containerView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
    containerView.alpha = 1.0
    containerView.clipsToBounds = true
    containerView.layer.cornerRadius = 10
    containerView.frame = CGRect(x: 0, y: 0, width: screenSize.width - 16, height: 50)
    containerView.center.x = view.center.x
    // If I add this UILabel as a subview the animation doesn't work
    let toastLbl = UILabel()
    toastLbl.translatesAutoresizingMaskIntoConstraints = false
    containerView.addSubview(toastLbl)
    toastLbl.textColor = UIColor.white
    toastLbl.font = .regularMontserrat(ofSize: 14)
    toastLbl.textAlignment = .center
    toastLbl.text = message
    toastLbl.numberOfLines = 1
    // If I add this UILabel as a subview the animation doesn't work
    view.addSubview(containerView)
    
    UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
        containerView.frame.origin.y = 50
    }, completion: { _ in
        UIView.animate(withDuration: 0.5, delay: 1.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
            containerView.frame.origin.y = -50
        }, completion: { _ in
            containerView.removeFromSuperview()
        })
    })
}

有办法解决这个问题吗?

共有1个答案

柴宏浚
2023-03-14

现在我再次尝试使用自动布局,它和我预期的一样工作。我想我给约束常量放了一个错误的值,所以它不能正常工作。谢谢@gereon的建议。

我将showtoast()函数分为两个独立的函数,如下所示:

func makeToast(message: String) -> UIView {
        let screenSize = UIScreen.main.bounds.size
        let containerView = UIView()
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.backgroundColor = .CREAM_ORANGE
        containerView.alpha = 1.0
        containerView.clipsToBounds = true
        containerView.layer.cornerRadius = 10
        containerView.frame = CGRect(x: 0, y: 0, width: screenSize.width - 32, height: 50)
        containerView.center.x = view.center.x
        containerView.alpha = 0
        let toastLbl = UILabel()
        toastLbl.translatesAutoresizingMaskIntoConstraints = false
        toastLbl.textColor = UIColor.white
        toastLbl.font = .regularMontserrat(ofSize: 14)
        toastLbl.textAlignment = .center
        toastLbl.text = message
        toastLbl.numberOfLines = 1
        view.addSubview(containerView)
        containerView.addSubview(toastLbl)
        NSLayoutConstraint.activate([
            containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
            containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
            containerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
            containerView.heightAnchor.constraint(equalToConstant: 50),
            toastLbl.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0),
            toastLbl.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0),
            toastLbl.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0),
            toastLbl.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0)
        ])
        return containerView
    }

func showToast(message: String) {
        let toast = makeToast(message: message)
        UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
            toast.alpha = 1
            toast.frame.origin.y = 50
        }, completion: { _ in
            UIView.animate(withDuration: 0.5, delay: 1.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
                toast.alpha = 0
                toast.frame.origin.y = -50
            }, completion: { _ in
                toast.removeFromSuperview()
            })
        })
    }
 类似资料:
  • 我正在当前视图底部的UIViewController中添加一个视图作为子视图。我可以使用alpha制作淡入淡出动画,但我想将其显示为类似键盘弹出窗口的幻灯片。 如何设置动画?

  • 代码显示了我的问题,我不能在。 错误消息是: /home/linuxbrew/。linuxbrew/Cellar/gcc/11.1。0_1/include/c/11.1。0/范围:1775:48:错误:传递'std::ranges::take_view

  • 我有一个UIView,其中有一个UILabel作为子视图。 现在我用动画改变UIView的宽度(改变约束的常量属性)并在UIView动画中调用layoutIfNeed...( 视图正在正常调整大小,但UILabel(子视图)将字体大小更改为“结束”大小,但没有像uiview那样正确设置动画。 根据WWDC 2012会话,如果仅更改superview的常量,则子视图应设置动画。 UILabel的最小

  • 我正在尝试做一个动画,并使用了下面的代码。我得到了“无法使用类型的参数列表调用'animateWithDuration'”(FloatLiteralConvertible,延迟:FloatLiteralConvertible,选项:UIViewAnimationOptions,animations:()- 这意味着我使用了错误的论点。我可能错了。请帮忙。我真的解决不了。 提前感谢。

  • 我正在尝试向我的菜单项(messageViewMenu)添加一个视图(messageView),这样当点击菜单项时,这个新视图就会添加到它的正上方,它们会一个接一个地在屏幕上向下滑动-Evernote 5.1.2有类似的功能。 我使用NSLayoutConstraint将新视图附加到我假定的菜单项顶部。然后,我将菜单项的现有垂直约束设置为标题UIView(\u headerMessageConst