隐藏属性无法在动画块中更改 [英] Hidden property cannot be changed within an animation block

查看:128
本文介绍了隐藏属性无法在动画块中更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UIStackView中嵌入了两个UILabel。顶部标签始终可见,但底部标签通过隐藏属性打开和关闭。我想要将此效果设置为动画,因此我将其粘贴在动画块中:

  private func toggleResultLabel(value:Double){ 
if value == 0 {
UIView.animateWithDuration(0.25){() - >无效
self.resultLabel.hidden = true
}
} else {
UIView.animateWithDuration(0.25){() - >无效
//奇怪的事情正在发生。我必须添加3个相同的语句来获取
//隐藏的标志为false
self.resultLabel.hidden = false
self.resultLabel.hidden = false
self .resultLabel.hidden = false
}
}
}

问题是隐藏属性不会改变,除非我反复重复该语句(在这种情况下为3次)。我在进入动画关闭时发现了这一点,并发现该属性不会改变它的赋值。现在我注意到看似随机发生的同样问题。如果相关,则第二个标签的默认值为 true



我在这里缺少什么,或者这是一个错误?



更新
对于它的价值,我通过添加 removeArrangedSubview()来实现它的工作 addArrangedSubview()

  if value = = 0 {
UIView.animateWithDuration(0.25){() - >无效
self.resultLabel.hidden = true
self.heroStackView.removeArrangedSubview(self.resultLabel)
}
} else {
UIView.animateWithDuration(0.25){ () - >无效
self.heroStackView.addArrangedSubview(self.resultLabel)
self.resultLabel.hidden = false
}
}


解决方案

在iOS 11及之前,当隐藏 arrangeSubview 时一个 UIStackView 多次使用UIView动画API,隐藏属性值为stack,它需要设置隐藏到 false 在值实际更改之前多次。



在工作中,我们决定使用UIView扩展和一个解决方法,该方法仅为给定值设置隐藏一次。

 扩展UIView {

// UIStackView错误的解决方法,其中使用动画设置隐藏为true
//多次为了显示视图,需要多次设置hidden to false。
public func workaround_nonRepeatingSetHidden(hidden:Bool){
if self.hidden!= hidden {
self.hidden = hidden
}
}
}

这绝对是UIKit中的一个错误,请查看


I've got two UILabels embedded within a UIStackView. The top label stays visible constantly, but the bottom label is toggled on and off via the hidden property. I wanted this effect to be animated, so I stuck it in an animation block:

private func toggleResultLabel(value:Double) {
    if value == 0 {
        UIView.animateWithDuration(0.25) { () -> Void in
            self.resultLabel.hidden = true
        }
    } else {
        UIView.animateWithDuration(0.25) { () -> Void in
            // Something weird is happening. I had to add 3 of the same statements to get 
            // the hidden flag to be false
            self.resultLabel.hidden = false
            self.resultLabel.hidden = false
            self.resultLabel.hidden = false
        }
    }
}

The problem is that the hidden property will not change unless I repeat the statement over and over (3 times in this case). I found this while breaking into the animation closure and seeing that the property would not change to it's assignment. Now I'm noticing the same problem occurring seemingly randomly again. The default value of the second label is true, if that's relevant.

Is there something I'm missing here, or is this a bug?

Update: For what it's worth, I got it working by adding removeArrangedSubview() and addArrangedSubview():

if value == 0 {
    UIView.animateWithDuration(0.25) { () -> Void in
        self.resultLabel.hidden = true
        self.heroStackView.removeArrangedSubview(self.resultLabel)
    }
 } else {
    UIView.animateWithDuration(0.25) { () -> Void in
        self.heroStackView.addArrangedSubview(self.resultLabel)
        self.resultLabel.hidden = false
    }
 }

解决方案

On iOS 11 and prior, when hiding an arrangedSubview of a UIStackView using UIView animation API multiple times, the hidden property values "stack", and it requires setting hidden to false multiple times before the value actually changes.

At work we decided to use a UIView extension with a workaround method that sets hidden only once for given value.

extension UIView {

    // Workaround for the UIStackView bug where setting hidden to true with animation
    // mulptiple times requires setting hidden to false multiple times to show the view.
    public func workaround_nonRepeatingSetHidden(hidden: Bool) {
        if self.hidden != hidden {
            self.hidden = hidden
        }
    }
}

This is definitely a bug in UIKit, check out the sample project that reproduces it clearly.

这篇关于隐藏属性无法在动画块中更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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