Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 隐藏和取消隐藏视图中的按钮时出现奇怪的动画_Swift_Constraints - Fatal编程技术网

Swift 隐藏和取消隐藏视图中的按钮时出现奇怪的动画

Swift 隐藏和取消隐藏视图中的按钮时出现奇怪的动画,swift,constraints,Swift,Constraints,我试图在堆栈视图中设置一个按钮按下时的进出动画。隐藏摄影机按钮时,动画看起来很好,但取消隐藏时,动画会从左侧滑入,这看起来很奇怪 代码如下: @IBAction func scanButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.3) { [self] in if cameraButton.alpha == 0{ cameraButton.alpha =

我试图在堆栈视图中设置一个按钮按下时的进出动画。隐藏摄影机按钮时,动画看起来很好,但取消隐藏时,动画会从左侧滑入,这看起来很奇怪

代码如下:

@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语句有多么糟糕)

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


感谢@aheze

您能在ishiden.toggle()之后调用stackView.layoutifneed()吗@aheze啊,我知道了,它说我需要嵌入10个声誉:/另外,最好将两个代码放在同一个动画块中。@ibrahimyilmaz非常感谢。。。它解决了我的问题。。我觉得很愚蠢这很正常因为这是一种意想不到的行为。我有这个想法是因为我以前也遇到过同样的问题。为你高兴。:)