Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update BWWalkthroughViewController.swift #131

Closed
wants to merge 2 commits into from
Closed

Update BWWalkthroughViewController.swift #131

wants to merge 2 commits into from

Conversation

relisiuol
Copy link

Added walkthroughDidScroll method to BWWalkthroughViewControllerDelegate.

Added walkthroughDidScroll method to BWWalkthroughViewControllerDelegate.
@relisiuol
Copy link
Author

relisiuol commented Dec 19, 2019

I use this delegate method to animate background color.

Returned percent variable, isn't a real percentage.

50 : current page visible only
Up to 50 : scroll to left (show previous page)
51 to 100 : scroll to right (show next page)

Assume you have defined walkthrough as BWWalkthroughViewController and walkthroughPagesBackgroundColor as [UIColor].

extension MyCustomViewController: BWWalkthroughViewControllerDelegate {
    func walkthroughDidScroll(percent: CGFloat) {
        guard let walkthrough = walkthrough,
            walkthrough.numberOfPages == walkthroughPagesBackgroundColor.count else {
            return
        }
        if percent == 50 {
            walkthrough.view.backgroundColor = walkthroughPagesBackgroundColor[walkthrough.currentPage]
        } else if percent > 50,
            (walkthrough.currentPage + 1) < walkthroughPages.count {
            let fromColor = walkthroughPagesBackgroundColor[walkthrough.currentPage]
            let toColor = walkthroughPagesBackgroundColor[(walkthrough.currentPage + 1)]
            let percentage = (percent - 50) * 2
            walkthrough.view.backgroundColor = fromColor.toColor(toColor, percentage: percentage).withAlphaComponent(0.8)
        } else if percent < 50,
            (walkthrough.currentPage - 1) >= 0 {
            let fromColor = walkthroughPagesBackgroundColor[walkthrough.currentPage]
            let toColor = walkthroughPagesBackgroundColor[(walkthrough.currentPage - 1)]
            let percentage = (50 - percent) * 2
            walkthrough.view.backgroundColor = fromColor.toColor(toColor, percentage: percentage)
        }
    }
}
extension UIColor {
    func toColor(_ color: UIColor, percentage: CGFloat) -> UIColor {
        let percentage = max(min(percentage, 100), 0) / 100
        switch percentage {
            case 0: return self
            case 1: return color
            default:
                var (r1, g1, b1, a1): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
                var (r2, g2, b2, a2): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
                guard self.getRed(&r1, green: &g1, blue: &b1, alpha: &a1) else { return self }
                guard color.getRed(&r2, green: &g2, blue: &b2, alpha: &a2) else { return self }
                return UIColor(red: CGFloat(r1 + (r2 - r1) * percentage),
                               green: CGFloat(g1 + (g2 - g1) * percentage),
                               blue: CGFloat(b1 + (b2 - b1) * percentage),
                               alpha: CGFloat(a1 + (a2 - a1) * percentage))
        }
    }
}

@relisiuol relisiuol closed this by deleting the head repository Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant