Estoy tratando de ocultar la subvista de UIStackView de esta manera:
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 2.0, delay: 0, options: [.curveEaseOut], animations: { self.label.isHidden = true self.label.alpha = 0.0 self.stackView.layoutIfNeeded() }) Sin embargo, la etiqueta desaparece instantáneamente con el uso de este código. Sospecho que esto se debe a la configuración de isHidden en verdadero, que se requiere para colapsar.
¿Hay alguna forma de ocultar y colapsar la vista secundaria de UIStackView con animación? ¿O podría ser mejor no usar UIStackView en absoluto?
Según la documentación de Apple :
Puede animar tanto los cambios en la propiedad isHidden de la subvista organizada como los cambios en las propiedades de la vista de pila colocando estos cambios dentro de un bloque de animación.
He probado el siguiente código con el simulador de iOS 12.1 y funciona como se esperaba.
UIView.animate( withDuration: 2.0, delay: 0.0, options: [.curveEaseOut], animations: { self.label.isHidden = true self.label.alpha = 0.0 })Puede animar propiedades de vista como alpha , color , etc. Sin embargo, algunas cosas suceden instantáneamente, en este caso isHidden .
Aquí hay un ejemplo usando 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 }) Usando 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 }He probado tu código. esta animando
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() } }