【问题标题】:Weird animation when hiding and unhiding button in UIStackView [duplicate]UIStackView中隐藏和取消隐藏按钮时的奇怪动画[重复]
【发布时间】:2021-02-10 01:35:08
【问题描述】:

当按下一个按钮时,我正在尝试在堆栈视图中对按钮进行动画处理。隐藏相机按钮时,动画看起来不错,但取消隐藏时它从左侧滑入,看起来很奇怪。

代码如下:

@IBAction func scanButtonTapped(_ sender: Any) {
    
    
    UIView.animate(withDuration: 0.3) { [self] in
        if cameraButton.alpha == 0{
            cameraButton.alpha = 1
        }else{
            cameraButton.alpha = 0
        }
    }
    
    UIView.animate(withDuration: 0.3) { [self] in
        cameraButton.isHidden.toggle()
        
    }
    
}

我尝试通过使用约束而不是堆栈来修复它,但无济于事。如果有人可以提供帮助,将不胜感激。 (请忽略 if/else 语句有多糟糕)

【问题讨论】:

  • 你可以尝试在isHidden.toggle()之后调用stackView.layoutIfNeeded()
  • @aheze 啊我明白了,它说我需要 10 声望才能嵌入:/
  • 另外,最好将两个代码放在同一个动画块中。
  • @ibrahimyilmaz 非常感谢......它解决了我的问题......我觉得很愚蠢
  • 这是正常的,因为这是一个意外的行为。我有这个想法是因为我以前也遇到过同样的情况。为你高兴。 :)

标签: swift constraints


【解决方案1】:

为@Ibrahimyilmaz 干杯。解决方案只需添加stackView.layoutIfNeeded()isHidden.toggle() 之后

Some more infomation on this@aheze 提供

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 2017-08-23
    相关资源
    最近更新 更多