From 7e084ece2ad5aa9309e9017aa24739e7d0452406 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 13 Oct 2014 01:16:40 +0900 Subject: [PATCH] remove RDPagingViewProtocol --- .../RDViewController.m | 2 + .../RDImageScrollView/RDImageScrollView.h | 2 +- .../RDImageScrollView/RDImageScrollView.m | 7 -- .../RDImageViewerController.m | 12 +++- Pod/Classes/RDPagingView/RDPagingView.h | 12 ++-- Pod/Classes/RDPagingView/RDPagingView.m | 71 +++++++++++++------ README.md | 18 ++++- 7 files changed, 84 insertions(+), 40 deletions(-) diff --git a/Example/RDImageViewerController/RDViewController.m b/Example/RDImageViewerController/RDViewController.m index 868fed0..7868723 100644 --- a/Example/RDImageViewerController/RDViewController.m +++ b/Example/RDImageViewerController/RDViewController.m @@ -84,6 +84,7 @@ - (IBAction)showViewController:(id)sender UILabel *label = [[UILabel alloc] initWithFrame:frame]; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont systemFontOfSize:50]; + label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; label.tag = 100; [view addSubview:label]; view.backgroundColor = [UIColor whiteColor]; @@ -154,6 +155,7 @@ - (IBAction)showImageAndView:(id)sender UILabel *label = [[UILabel alloc] initWithFrame:frame]; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont systemFontOfSize:50]; + label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; label.tag = 100; [view addSubview:label]; view.backgroundColor = [UIColor whiteColor]; diff --git a/Pod/Classes/RDImageScrollView/RDImageScrollView.h b/Pod/Classes/RDImageScrollView/RDImageScrollView.h index 31c7475..8f29904 100644 --- a/Pod/Classes/RDImageScrollView/RDImageScrollView.h +++ b/Pod/Classes/RDImageScrollView/RDImageScrollView.h @@ -15,7 +15,7 @@ @end -@interface RDImageScrollView : UIScrollView +@interface RDImageScrollView : UIScrollView @property (nonatomic, getter=image, setter=setImage:) UIImage *image; diff --git a/Pod/Classes/RDImageScrollView/RDImageScrollView.m b/Pod/Classes/RDImageScrollView/RDImageScrollView.m index df3e710..c5cdd53 100644 --- a/Pod/Classes/RDImageScrollView/RDImageScrollView.m +++ b/Pod/Classes/RDImageScrollView/RDImageScrollView.m @@ -207,11 +207,4 @@ - (void)scrollViewDidZoom:(UIScrollView *)scrollView subView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX, scrollView.contentSize.height * 0.5 + offsetY); } -#pragma mark - RDPagingViewProtocol - -- (NSInteger)indexOfPage -{ - return self.tag - RDSubViewTagOffset; -} - @end diff --git a/Pod/Classes/RDImageViewerController/RDImageViewerController.m b/Pod/Classes/RDImageViewerController/RDImageViewerController.m index f4ffaed..ee48d82 100644 --- a/Pod/Classes/RDImageViewerController/RDImageViewerController.m +++ b/Pod/Classes/RDImageViewerController/RDImageViewerController.m @@ -429,10 +429,20 @@ - (UIView *)pagingView:(RDPagingView *)pageView viewForIndex:(NSInteger)index return view; } +- (NSString *)paginView:(RDPagingView *)paginView reuseIdentifierForIndex:(NSInteger)index +{ + NSString *identifier = RDImageViewerControllerReuseIdentifierImage; + if (self.reuseIdentifierHandler) { + identifier = self.reuseIdentifierHandler(index); + } + + return identifier; +} + - (void)pagingView:(RDPagingView *)pagingView willChangeViewSize:(CGSize)size duration:(NSTimeInterval)duration visibleViews:(NSArray *)views { [views enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - __block RDImageScrollView *v = obj; + __block RDImageScrollView *v = obj; if (v.indexOfPage != pagingView.currentPageIndex) { v.hidden = YES; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ diff --git a/Pod/Classes/RDPagingView/RDPagingView.h b/Pod/Classes/RDPagingView/RDPagingView.h index 753b1fb..cae5ffc 100644 --- a/Pod/Classes/RDPagingView/RDPagingView.h +++ b/Pod/Classes/RDPagingView/RDPagingView.h @@ -20,21 +20,19 @@ typedef NS_ENUM(NSInteger, RDPagingViewMovingDirection) { RDPagingViewMovingDirectionBackward = -1 }; -@class RDPagingView; - -extern NSInteger const RDSubViewTagOffset; - -@protocol RDPagingViewProtocol +@interface UIView (RDPagingView) -@required -- (NSInteger)indexOfPage; +@property (nonatomic, readonly) NSInteger indexOfPage; @end +@class RDPagingView; + @protocol RDPagingViewDelegate @required - (UIView *)pagingView:(RDPagingView *)pageView viewForIndex:(NSInteger)index; +- (NSString *)paginView:(RDPagingView *)paginView reuseIdentifierForIndex:(NSInteger)index; @optional - (void)pagingView:(RDPagingView *)pagingView willChangeViewSize:(CGSize)size duration:(NSTimeInterval)duration visibleViews:(NSArray *)views; diff --git a/Pod/Classes/RDPagingView/RDPagingView.m b/Pod/Classes/RDPagingView/RDPagingView.m index 56f70c8..d1c96c0 100644 --- a/Pod/Classes/RDPagingView/RDPagingView.m +++ b/Pod/Classes/RDPagingView/RDPagingView.m @@ -7,6 +7,7 @@ // #import "RDPagingView.h" +#import typedef struct { BOOL pagingViewWillChangeViewSize; @@ -20,11 +21,27 @@ BOOL pagingViewDidEndScrollingAnimation; } RDPagingViewDelegateFlag; +@implementation UIView (RDPagingView) + +static NSString *const kRDPagingViewIndexOfPage = @"RDPagingViewIndexOfPage"; +@dynamic indexOfPage; + +- (NSInteger)indexOfPage +{ + return [(NSNumber *)objc_getAssociatedObject(self, (__bridge const void *)(kRDPagingViewIndexOfPage)) integerValue]; +} + +- (void)setIndexOfPage:(NSInteger)indexOfPage +{ + objc_setAssociatedObject(self, (__bridge const void *)(kRDPagingViewIndexOfPage), @(indexOfPage), OBJC_ASSOCIATION_RETAIN); +} + +@end + @interface RDPagingView () { NSMutableDictionary *queueDictionary_; NSMutableSet *usingViews_; - NSMutableSet *preparedViews_; NSInteger pageIndex_; RDPagingViewDelegateFlag flag_; id delegate_; @@ -34,7 +51,6 @@ @interface RDPagingView () @implementation RDPagingView -NSInteger const RDSubViewTagOffset = 3; static NSInteger const kPreloadDefaultCount = 1; - (instancetype)initWithFrame:(CGRect)frame @@ -50,7 +66,6 @@ - (instancetype)initWithFrame:(CGRect)frame queueDictionary_ = [NSMutableDictionary new]; usingViews_ = [NSMutableSet new]; - preparedViews_ = [NSMutableSet new]; } return self; @@ -106,19 +121,19 @@ - (NSInteger)indexInScrollView:(NSInteger)index return trueIndex; } -- (void)setViewPrepared:(UIView *)view +- (void)setViewPrepared:(UIView *)view reuseIdentifier:(NSString *)identifier { if (view) { [view removeFromSuperview]; [usingViews_ removeObject:view]; - [preparedViews_ addObject:view]; + [queueDictionary_[identifier] addObject:view]; } } -- (void)setViewUsing:(UIView *)view +- (void)setViewUsing:(UIView *)view reuseIdentifier:(NSString *)identifier { if (view) { - [preparedViews_ removeObject:view]; + [queueDictionary_[identifier] removeObject:view]; [usingViews_ addObject:view]; } } @@ -170,21 +185,18 @@ - (void)scrollAtPage:(NSInteger)page - (void)pageIndexWillChangeToIndex:(NSInteger)index { NSInteger direction = index - self.currentPageIndex > 0 ? RDPagingViewMovingDirectionForward : (index - self.currentPageIndex < 0 ? RDPagingViewMovingDirectionBackward : 0); - NSInteger offset = index - (direction * (_preloadCount + 1)) + RDSubViewTagOffset; - if (offset > 0) { - NSInteger maximumIndex = index + _preloadCount + RDSubViewTagOffset; - NSInteger minimumIndex = index - (NSInteger)_preloadCount + RDSubViewTagOffset; - [usingViews_ enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { - UIView *view = obj; - if (view.tag < minimumIndex || view.tag > maximumIndex) { - [self setViewPrepared:view]; - } - }]; - - if (direction != 0) { - [self preloadWithNumberOfViews:_preloadCount fromIndex:index]; + NSInteger maximumIndex = index + _preloadCount; + NSInteger minimumIndex = index - _preloadCount; + [usingViews_ enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { + UIView *view = obj; + if (view.indexOfPage < minimumIndex || view.indexOfPage > maximumIndex) { + [self setViewPrepared:view reuseIdentifier:[self.pagingDelegate paginView:self reuseIdentifierForIndex:view.indexOfPage]]; } + }]; + + if (direction != 0) { + [self preloadWithNumberOfViews:_preloadCount fromIndex:index]; } if (flag_.pagingViewWillChangeIndexTo) { @@ -195,7 +207,7 @@ - (void)pageIndexWillChangeToIndex:(NSInteger)index - (void)preloadWithNumberOfViews:(NSInteger)num fromIndex:(NSInteger)index { for (NSInteger i = MAX(index - num, 0); i < MIN(index + num + 1, self.numberOfPages); i++) { - if ([self viewWithTag:i + RDSubViewTagOffset] == nil) { + if ([self viewForIndex:i] == nil) { [self loadViewAtIndex:i]; } } @@ -205,11 +217,24 @@ - (void)loadViewAtIndex:(NSInteger)index { UIView *view = [self.pagingDelegate pagingView:self viewForIndex:index]; view.frame = CGRectMake([self indexInScrollView:index] * CGRectGetWidth(self.frame), 0, CGRectGetWidth(self.frame) ,CGRectGetHeight(self.frame)); - view.tag = index + RDSubViewTagOffset; - [self setViewUsing:view]; + [view setIndexOfPage:index]; + [self setViewUsing:view reuseIdentifier:[self.pagingDelegate paginView:self reuseIdentifierForIndex:index]]; [self addSubview:view]; } +- (UIView *)viewForIndex:(NSInteger)index +{ + UIView *result = nil; + for (UIView *view in usingViews_) { + if (view.indexOfPage == index) { + result = view; + break; + } + } + + return result; +} + #pragma mark UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView diff --git a/README.md b/README.md index 9ebc0bb..0bcc852 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # RDImageViewerController +Simple image or custom view viewer. + [![CI Status](http://img.shields.io/travis/Akira Matsuda/RDImageViewerController.svg?style=flat)](https://travis-ci.org/Akira Matsuda/RDImageViewerController) [![Version](https://img.shields.io/cocoapods/v/RDImageViewerController.svg?style=flat)](http://cocoadocs.org/docsets/RDImageViewerController) [![License](https://img.shields.io/cocoapods/l/RDImageViewerController.svg?style=flat)](http://cocoadocs.org/docsets/RDImageViewerController) @@ -30,6 +32,20 @@ Initialize RDImageViewerController with number of images and image handler. return [UIImage imageNamed:imageName]; } numberOfImage:10 direction:RDPagingViewDirectionRight]; +You can show a custom view like this. + + RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithViewHandler:^UIView *(NSInteger pageIndex, UIView *reusedView) { + if (reusedView == nil) { + // create new view + } + // customize view + return reusedView; + } reuseIdentifier:^NSString *(NSInteger pageIndex) { + return @"view"; + } numberOfImages:10 direction:RDPagingViewDirectionRight]; + +Please check the sample code to know how to use. + To load asynchronous, do this RDImageViewerController *viewController = ... @@ -50,7 +66,7 @@ You can set the number of preloading images like this. ![](https://raw.githubusercontent.com/0x0c/RDImageViewerController/master/Example/Screenshot/3.png) ![](https://raw.githubusercontent.com/0x0c/RDImageViewerController/master/Example/Screenshot/4.png) -The image view will be reused when scrolled. +The view will be reused when scrolled. ## Author