Created
January 18, 2019 01:36
-
-
Save Aymenworks/674d43a7fec39d5135e231409cea3787 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
// MARK: Properties | |
@IBOutlet weak var scrollView: UIScrollView! | |
@IBOutlet weak var searchView: SearchView! | |
let goal: CGFloat = 50 | |
var observeScrollViewContentOffset: NSKeyValueObservation? | |
// MARK: Lifecycle | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
observeScrollViewContentOffset = scrollView.observe(\.contentOffset, options: .new) { [weak self] (scrollView, change) in | |
guard let `self` = self, let newValue = change.newValue?.y else { return } | |
// Remove negative value | |
var progress = -newValue/self.goal | |
// Be sure that the progress is always between 0 and 1 | |
progress = min(max(0, progress), 1) | |
self.searchView.update(progress: progress) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment