Reputation: 8153
I'm trying to perform alpha animation of subview when UIScrollView
is scrolled certain amount. I have UIView
which has another UIView
as child.
Below variable is changed to trigger the animation
var isExpanded: Bool = false {
didSet {
if (oldValue != isExpanded) {
toggleExpanded()
}
}
}
I have tried option 1:
func toggleExpanded() {
UIView.animate(withDuration: 300 , animations: {
if (self.isExpanded) {
self.mySubView.alpha = 1.0
} else {
self.mySubView.alpha = 0.0
}
})
}
And option 2:
func toggleExpanded() {
if (isExpanded) {
mySubView.alpha = 1.0
} else {
mySubView.alpha = 0.0
}
UIView.animate(withDuration: 300 , animations: {
self.layoutIfNeeded()
})
}
Either one seem to work, alpha changes immediately and in option 1 it seems to even be opposite. Can for example auto layout constraints affect animations here? Where to start when encountering this kind of issue?
Thanks.
Upvotes: 0
Views: 480
Reputation: 6110
Your first solution is right, but the problem is with the duration. duration
is measured in seconds, try with 1 or 0.5 second and everything will be fine.
Check the documentation of animate(withDuration:animations:completion:)
for more information.
Upvotes: 2