Skip to content

Instantly share code, notes, and snippets.

@hsjunnesson
Last active August 29, 2015 14:01
Show Gist options
  • Save hsjunnesson/c11cb403e0cfc2a8432b to your computer and use it in GitHub Desktop.
Save hsjunnesson/c11cb403e0cfc2a8432b to your computer and use it in GitHub Desktop.
Using ReactiveCocoa to get rid of the scroll view delegate, to update a UIPageControl's currentPage as you scroll.
CGFloat pageWidth = CGRectGetWidth(self.scrollView.frame);
RAC(self.pageControl, currentPage) = [RACObserve(self.scrollView, contentOffset) map:^id(NSValue *value) {
CGPoint contentOffset = [value CGPointValue];
NSUInteger page = floor((contentOffset.x - pageWidth / 2) / pageWidth) + 1;
return @(page);
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment