From 26b7653db128a38f6ba034c3d4490e2ce4176940 Mon Sep 17 00:00:00 2001 From: Xiaoming Date: Mon, 13 Jun 2016 15:15:19 +0800 Subject: [PATCH] disable UIPageViewController's bounce --- Source/OnboardingViewController.h | 6 ++++++ Source/OnboardingViewController.m | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Source/OnboardingViewController.h b/Source/OnboardingViewController.h index bf63200..7fa19e1 100644 --- a/Source/OnboardingViewController.h +++ b/Source/OnboardingViewController.h @@ -102,6 +102,12 @@ @property (nonatomic) CGFloat underPageControlPadding; +/** + * @brief Determines whether or not disable UIPageViewController's bounce. The default value of this property is NO. + */ +@property (nonatomic) BOOL disableBounce; + + /** * @brief Convenience class initializer for onboarding with a backround image. * @return An instance of OnboardingViewController with the provided background image and content view controllers. diff --git a/Source/OnboardingViewController.m b/Source/OnboardingViewController.m index 481c400..7172158 100644 --- a/Source/OnboardingViewController.m +++ b/Source/OnboardingViewController.m @@ -94,6 +94,7 @@ - (instancetype)initWithContents:(NSArray *)contents { self.fadePageControlOnLastPage = NO; self.fadeSkipButtonOnLastPage = NO; self.swipingEnabled = YES; + self.disableBounce = NO; self.allowSkipping = NO; self.skipHandler = ^{}; @@ -467,6 +468,24 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView { _skipButton.alpha = percentComplete; } } + + if (self.disableBounce) { + if (_currentPage == self.viewControllers.firstObject && scrollView.contentOffset.x <= scrollView.bounds.size.width) { + scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0); + } else if (_currentPage == self.viewControllers.lastObject && scrollView.contentOffset.x >= scrollView.bounds.size.width) { + scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0); + } + } +} + +- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { + if (self.disableBounce) { + if (_currentPage == self.viewControllers.firstObject && scrollView.contentOffset.x <= scrollView.bounds.size.width) { + *targetContentOffset = CGPointMake(scrollView.bounds.size.width, 0); + } else if (_currentPage == self.viewControllers.lastObject && scrollView.contentOffset.x >= scrollView.bounds.size.width) { + *targetContentOffset = CGPointMake(scrollView.bounds.size.width, 0); + } + } }