简体   繁体   中英

Change UIButton tint color while scrolling

I have a custom table view header that becomes blurred when the user scrolls down. Id like to change the tint color of one of the navigation bar items items (its not a nav bar button item, just a UIButton). I have been using the tableviews contentOffset.y property to animate buttons, and blur the background image view. I need to do this to the tint color as well but I can figure out how.

For example this is code to shrink the profile pic and the hide some views using the alpha property

func movingUp(offset: CGFloat) {
    if infoView.alpha >= 0 {
        let alphaOffset = (offset - 133)/105.0
        infoView.alpha = alphaOffset*1.2
        profilePic.alpha = alphaOffset
    }

    if profileWidth.constant >= 45 {
        profilePic.layer.cornerRadius = (((offset - 133)/105.0) * 90)/2
        profileHeight.constant = ((offset - 133)/105.0) * 90
        profileWidth.constant = ((offset - 133)/105.0) * 90
    }
}

I have an answer that will help you here... http://www.oliverfoggin.com/controlling-animations-with-a-uiscrollview/

The idea is that you can move between two extreme values by using the current content offset and using it as a percentage based on the maximum offset.

In your case, it's easy as the colour is a simple inverse function of the percentage.

ie

let color = UIColor(white: 1.0 - percentage, alpha: 1.0)

When the percentage is 0.0 (ie top of the table) then the white value is 1.0 = white.

When the percentage is 1.0 (ie bottom of the table) then the white value is 0.0 = black.

I just realised that my post is in Objective-C. It should be very easy to convert to Swift as it just uses simple math functions. If you'd like me to convert it let me know and I'll put it here.

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