代码之家  ›  专栏  ›  技术社区  ›  Andrey Gordeev

uistackview-用动画隐藏和折叠子视图

  •  4
  • Andrey Gordeev  · 技术社区  · 7 年前

    我试图隐藏uistackview的子视图,如下所示:

    UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 2.0, 
          delay: 0, options: [.curveEaseOut], animations: {
        self.label.isHidden = true
        self.label.alpha = 0.0
        self.stackView.layoutIfNeeded()
    })
    

    但是,使用此代码后,标签会立即消失。我怀疑这是因为 isHidden 为真,这是坍塌所必需的。

    有没有办法用动画隐藏和折叠uistackview的子视图?或者最好不要使用uistackview?

    5 回复  |  直到 7 年前
        1
  •  6
  •   Wez    7 年前

    根据 Apple's documentation :

    通过将这些更改放置在动画块中,可以对排列的子视图的ishidden属性的更改和对堆栈视图的属性的更改进行动画处理。

    我已经使用iOS12.1模拟器测试了下面的代码,它工作正常。

    UIView.animate(
        withDuration: 2.0,
        delay: 0.0,
        options: [.curveEaseOut],
        animations: {
            self.label.isHidden = true
            self.label.alpha = 0.0
    })
    

    Arranged Subview Animation Gif


    它似乎是设置约束动画的经典方法 layoutIfNeeded() 不适用于已安排好的子视图,谁知道。

        2
  •  3
  •   ZGski Adam Smith    7 年前

    您可以将视图属性设置为 alpha , color 等等。但是,有些事情会立即发生。- isHidden 在这种情况下。

    下面是一个使用 UIView.animate :

    UIView.animate(withDuration: 2, delay: 0, options: .curveEaseOut, animations: {
        self.label.alpha = 0 // Changes the label's layer alpha value
    }, completion: { finished in
        self.label.isHidden = true // Hides the label
        self.label.layer.alpha = 1 // Resets the label's alpha without un-hiding it
    })
    

    使用 UIViewPropertyAnimator :

    UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 2, delay: 0, options: .curveEaseOut, animations: {
        self.label.alpha = 0 // Sets the label's alpha
    }) { _ in
        self.label.isHidden = true // Hides the label
        self.label.alpha = 1 // Resets the label's alpha without un-hiding it
    }
    
        3
  •  1
  •   Kathiresan Murugan    7 年前

    我试过你的密码。它的动画

    if self.stackView.subviews.count > 0 {
                UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1.0, delay: 0, options: [.curveEaseOut], animations: {
    
                    self.stackView.subviews[0].isHidden = true
                    self.stackView.subviews[0].alpha = 0.0
                    self.stackView.layoutIfNeeded()
                }) { (position) in
                    self.stackView.subviews[0].removeFromSuperview()
                }
            }
    

    initialScreen

    animated

        4
  •  0
  •   Sand'sHell811    7 年前

    确保没有对StackView给定高度约束。 试试这个。

    UIView.animate(withDuration: 0.5) {
       self.stackView.subviews[INDEX_OF_LABEL_IN_STACK]?.alpha = 0
       self.stackView.subviews[INDEX_OF_LABEL_IN_STACK]?.isHidden = true
       self.view.layoutSubviews()
    }
    
        5
  •  0
  •   Abdelahad Darwish    7 年前

    只是你可以用简单的解决方案 animateKeyframes 为了使alpha褪色,然后隐藏,我想这会给你所需要的,所以在1秒和0.8秒后隐藏

    //ShowLabel是用于处理状态的bool,在文件中声明它

    @IBAction func toggleStackLabelTapped(_ sender: UIButton) {
    
        showLabel = !showLabel
    
        UIView.animateKeyframes(withDuration: 1, delay: 0, options: .calculationModeLinear, animations: {
            UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.8) {
                self.label.alpha =  (self.showLabel) ? 1 : 0
            }
            UIView.addKeyframe(withRelativeStartTime: 0.8, relativeDuration: 1) {
                self.label.isHidden = !self.showLabel
            }
    
        })
    }