简体   繁体   中英

UIView subView alpha animation not working

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM