From 9052794fdfd817859be02b00f2b0c5144abd660f Mon Sep 17 00:00:00 2001 From: DreamingInBinary Date: Fri, 12 Jan 2018 14:29:09 -0600 Subject: [PATCH 1/4] Hook into scrollview for percent complete --- .../BFRImageViewController.m | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/BFRImageViewController/BFRImageViewController.m b/BFRImageViewController/BFRImageViewController.m index c28d9c9..1c74001 100644 --- a/BFRImageViewController/BFRImageViewController.m +++ b/BFRImageViewController/BFRImageViewController.m @@ -12,7 +12,7 @@ #import "BFRImageTransitionAnimator.h" #import "BFRImageViewerConstants.h" -@interface BFRImageViewController () +@interface BFRImageViewController () /*! This view controller just acts as a container to hold a page view controller, which pages between the view controllers that hold an image. */ @property (strong, nonatomic, nonnull) UIPageViewController *pagerVC; @@ -106,6 +106,14 @@ - (void)viewDidLoad { [[self view] addSubview:[self.pagerVC view]]; [self.pagerVC didMoveToParentViewController:self]; + // Attach to pager controller's scrollview for parallax effect when swiping between images + for (UIView *subview in self.pagerVC.view.subviews) { + if ([subview isKindOfClass:[UIScrollView class]]) { + ((UIScrollView *)subview).delegate = self; + break; + } + } + // Add chrome to UI now if we aren't waiting to be peeked into if (!self.isBeingUsedFor3DTouch) { [self addChromeToUI]; @@ -200,6 +208,14 @@ - (UIViewController *)pageViewController:(UIPageViewController *)pageViewControl return vc; } +#pragma mark - Scrollview Delegate + +- (void)scrollViewDidScroll:(UIScrollView *)scrollView { + CGPoint point = scrollView.contentOffset; + CGFloat percentComplete = fabs(point.x - self.view.frame.size.width)/self.view.frame.size.width; + NSLog(@"Percent scrolled %f", percentComplete); +} + #pragma mark - Utility methods - (void)dismiss { // If we dismiss from a different image than what was animated in - don't do the custom dismiss transition animation From fb01df26d48f27c2a57e959c845b867fb3b6c003 Mon Sep 17 00:00:00 2001 From: DreamingInBinary Date: Tue, 16 Jan 2018 09:41:35 -0600 Subject: [PATCH 2/4] Parallax view, correct coding style for pragma marks --- BFRImageViewController/BFRBackLoadedImageSource.m | 2 ++ .../BFRImageContainerViewController.m | 9 ++++++++- BFRImageViewController/BFRImageTransitionAnimator.m | 4 ++++ BFRImageViewController/BFRImageViewController.m | 12 ++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/BFRImageViewController/BFRBackLoadedImageSource.m b/BFRImageViewController/BFRBackLoadedImageSource.m index 7b1ce39..164f778 100644 --- a/BFRImageViewController/BFRBackLoadedImageSource.m +++ b/BFRImageViewController/BFRBackLoadedImageSource.m @@ -25,6 +25,7 @@ @interface BFRBackLoadedImageSource() @implementation BFRBackLoadedImageSource #pragma mark - Initializers + - (instancetype)initWithInitialImage:(UIImage *)image hiResURL:(NSURL *)url { self = [super init]; @@ -39,6 +40,7 @@ - (instancetype)initWithInitialImage:(UIImage *)image hiResURL:(NSURL *)url { } #pragma mark - Backloading + - (void)loadHighFidelityImage { [[PINRemoteImageManager sharedImageManager] downloadImageWithURL:self.url options:PINRemoteImageManagerDisallowAlternateRepresentations progressDownload:nil completion:^(PINRemoteImageManagerResult * _Nonnull result) { dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/BFRImageViewController/BFRImageContainerViewController.m b/BFRImageViewController/BFRImageContainerViewController.m index a775ac2..afd017b 100644 --- a/BFRImageViewController/BFRImageContainerViewController.m +++ b/BFRImageViewController/BFRImageContainerViewController.m @@ -42,6 +42,7 @@ @interface BFRImageContainerViewController () )transitionContext { return self.animationDuration; } @@ -170,6 +173,7 @@ - (void)performDismissingAnimation:(id)tra } #pragma mark - Transitioning Delegate + - (id )animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source { return self; } diff --git a/BFRImageViewController/BFRImageViewController.m b/BFRImageViewController/BFRImageViewController.m index 1c74001..8a1bc1c 100644 --- a/BFRImageViewController/BFRImageViewController.m +++ b/BFRImageViewController/BFRImageViewController.m @@ -35,11 +35,15 @@ @interface BFRImageViewController () Date: Tue, 16 Jan 2018 10:02:58 -0600 Subject: [PATCH 3/4] First go parallax effect --- .../BFRImageViewController.m | 40 +++++++++++++++++-- .../BFRImageViewerConstants.h | 3 ++ .../BFRImageViewerConstants.m | 2 + 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/BFRImageViewController/BFRImageViewController.m b/BFRImageViewController/BFRImageViewController.m index 8a1bc1c..d5f99d1 100644 --- a/BFRImageViewController/BFRImageViewController.m +++ b/BFRImageViewController/BFRImageViewController.m @@ -117,6 +117,13 @@ - (void)viewDidLoad { for (UIView *subview in self.pagerVC.view.subviews) { if ([subview isKindOfClass:[UIScrollView class]]) { ((UIScrollView *)subview).delegate = self; + self.parallaxView.backgroundColor = self.view.backgroundColor; + [subview addSubview:self.parallaxView]; + + CGRect parallaxSeparatorFrame = CGRectZero; + parallaxSeparatorFrame.size = [self sizeForParallaxView]; + self.parallaxView.frame = parallaxSeparatorFrame; + break; } } @@ -173,6 +180,7 @@ - (void)addChromeToUI { } } +//TODO: Update parallax effect view - (void)updateChromeFrames { if (self.enableDoneButton) { CGFloat buttonX = self.showDoneButtonOnLeft ? 20 : CGRectGetMaxX(self.view.bounds) - 37; @@ -218,12 +226,36 @@ - (UIViewController *)pageViewController:(UIPageViewController *)pageViewControl return vc; } -#pragma mark - Scrollview Delegate +#pragma mark - Scrollview Delegate + Parallax Effect - (void)scrollViewDidScroll:(UIScrollView *)scrollView { - CGPoint point = scrollView.contentOffset; - CGFloat percentComplete = fabs(point.x - self.view.frame.size.width)/self.view.frame.size.width; - NSLog(@"Percent scrolled %f", percentComplete); + [self updateParallaxViewFrame:scrollView]; +} + +- (void)updateParallaxViewFrame:(UIScrollView *)scrollView { + CGRect bounds = scrollView.bounds; + CGRect parallaxSeparatorFrame = self.parallaxView.frame; + + CGPoint offset = bounds.origin; + CGFloat pageWidth = bounds.size.width; + + NSInteger firstPageIndex = floorf(CGRectGetMinX(bounds) / pageWidth); + + CGFloat x = offset.x - pageWidth * firstPageIndex; + CGFloat percentage = x / pageWidth; + + parallaxSeparatorFrame.origin.x = pageWidth * (firstPageIndex + 1) - parallaxSeparatorFrame.size.width * percentage; + + self.parallaxView.frame = parallaxSeparatorFrame; +} + +- (CGSize)sizeForParallaxView { + CGSize parallaxSeparatorSize = CGSizeZero; + + parallaxSeparatorSize.width = PARALLAX_EFFECT_WIDTH * 2; + parallaxSeparatorSize.height = self.view.bounds.size.height; + + return parallaxSeparatorSize; } #pragma mark - Utility methods diff --git a/BFRImageViewController/BFRImageViewerConstants.h b/BFRImageViewController/BFRImageViewerConstants.h index 23bf764..aa92d20 100644 --- a/BFRImageViewController/BFRImageViewerConstants.h +++ b/BFRImageViewController/BFRImageViewerConstants.h @@ -25,4 +25,7 @@ extern NSString * const GENERAL_OK; extern NSString * const HI_RES_IMG_ERROR_DOMAIN; extern NSInteger const HI_RES_IMG_ERROR_CODE; +// Misc +extern NSInteger const PARALLAX_EFFECT_WIDTH; + @end diff --git a/BFRImageViewController/BFRImageViewerConstants.m b/BFRImageViewController/BFRImageViewerConstants.m index be86b4e..5191512 100644 --- a/BFRImageViewController/BFRImageViewerConstants.m +++ b/BFRImageViewController/BFRImageViewerConstants.m @@ -23,4 +23,6 @@ @implementation BFRImageViewerConstants NSString * const HI_RES_IMG_ERROR_DOMAIN = @"com.bfrImageViewer.backLoadedImgSource"; NSInteger const HI_RES_IMG_ERROR_CODE = 44; +NSInteger const PARALLAX_EFFECT_WIDTH = 20; + @end From 7a7b990187f7980f11c719f43cc437fb4c47995b Mon Sep 17 00:00:00 2001 From: DreamingInBinary Date: Tue, 16 Jan 2018 10:30:21 -0600 Subject: [PATCH 4/4] Fix hidden state of parallax view --- BFRImageViewController/BFRImageViewController.m | 10 ++++++++-- .../BFRImageViewer/ThirdViewController.m | 6 +++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/BFRImageViewController/BFRImageViewController.m b/BFRImageViewController/BFRImageViewController.m index d5f99d1..756c09b 100644 --- a/BFRImageViewController/BFRImageViewController.m +++ b/BFRImageViewController/BFRImageViewController.m @@ -118,6 +118,7 @@ - (void)viewDidLoad { if ([subview isKindOfClass:[UIScrollView class]]) { ((UIScrollView *)subview).delegate = self; self.parallaxView.backgroundColor = self.view.backgroundColor; + self.parallaxView.hidden = YES; [subview addSubview:self.parallaxView]; CGRect parallaxSeparatorFrame = CGRectZero; @@ -145,7 +146,7 @@ - (void)viewWillAppear:(BOOL)animated { }]; } -- (void)viewWillLayoutSubviews { +- (void)viewDidLayoutSubviews { [super viewWillLayoutSubviews]; [self updateChromeFrames]; } @@ -180,7 +181,6 @@ - (void)addChromeToUI { } } -//TODO: Update parallax effect view - (void)updateChromeFrames { if (self.enableDoneButton) { CGFloat buttonX = self.showDoneButtonOnLeft ? 20 : CGRectGetMaxX(self.view.bounds) - 37; @@ -192,6 +192,8 @@ - (void)updateChromeFrames { self.doneButton.frame = CGRectMake(buttonX, closeButtonY, 17, 17); } + + self.parallaxView.hidden = YES; } #pragma mark - Pager Datasource @@ -228,6 +230,10 @@ - (UIViewController *)pageViewController:(UIPageViewController *)pageViewControl #pragma mark - Scrollview Delegate + Parallax Effect +- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { + self.parallaxView.hidden = NO; +} + - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [self updateParallaxViewFrame:scrollView]; } diff --git a/BFRImageViewerDemo/BFRImageViewer/ThirdViewController.m b/BFRImageViewerDemo/BFRImageViewer/ThirdViewController.m index 73be809..05f81d6 100644 --- a/BFRImageViewerDemo/BFRImageViewer/ThirdViewController.m +++ b/BFRImageViewerDemo/BFRImageViewer/ThirdViewController.m @@ -32,8 +32,8 @@ - (void)viewDidLoad { // To use the custom transition animation with BFRImageViewer // 1) Have an instance of BFRImageTransitionAnimator around - // 2) Set it's aniamtedImage, animatedImageContainer and imageOriginFrame. Optionally, set the desiredContentMode - // 3) When you present the BFRImageViewController, set it's transitioningDelegate to your BFRImageTransitionAnimator instance. + // 2) Set its animatedImage, animatedImageContainer and imageOriginFrame. Optionally, set the desiredContentMode + // 3) When you present the BFRImageViewController, set its transitioningDelegate to your BFRImageTransitionAnimator instance. // You can see all of this in action in openImageViewerWithTransition below // Object to create all the animations @@ -68,7 +68,7 @@ - (void)openImageViewerWithTransition { self.imageViewAnimator.animatedImageContainer = self.imageView; // The image that will be animated self.imageViewAnimator.animatedImage = self.imageView.image; - // The rect the image will aniamte to and from + // The rect the image will animate to and from self.imageViewAnimator.imageOriginFrame = self.imageView.frame; // Optional - but you'll want this to match the view's content mode that the image is housed in self.imageViewAnimator.desiredContentMode = self.imageView.contentMode;