Skip to content

Commit

Permalink
remove RDPagingViewProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0c committed Oct 12, 2014
1 parent ec72446 commit 7e084ec
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 40 deletions.
2 changes: 2 additions & 0 deletions Example/RDImageViewerController/RDViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion Pod/Classes/RDImageScrollView/RDImageScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@end

@interface RDImageScrollView : UIScrollView <UIScrollViewDelegate, RDPagingViewProtocol>
@interface RDImageScrollView : UIScrollView <UIScrollViewDelegate>

@property (nonatomic, getter=image, setter=setImage:) UIImage *image;

Expand Down
7 changes: 0 additions & 7 deletions Pod/Classes/RDImageScrollView/RDImageScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 11 additions & 1 deletion Pod/Classes/RDImageViewerController/RDImageViewerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<RDPagingViewProtocol> *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(), ^{
Expand Down
12 changes: 5 additions & 7 deletions Pod/Classes/RDPagingView/RDPagingView.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,19 @@ typedef NS_ENUM(NSInteger, RDPagingViewMovingDirection) {
RDPagingViewMovingDirectionBackward = -1
};

@class RDPagingView;

extern NSInteger const RDSubViewTagOffset;

@protocol RDPagingViewProtocol <NSObject>
@interface UIView (RDPagingView)

@required
- (NSInteger)indexOfPage;
@property (nonatomic, readonly) NSInteger indexOfPage;

@end

@class RDPagingView;

@protocol RDPagingViewDelegate <NSObject>

@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;
Expand Down
71 changes: 48 additions & 23 deletions Pod/Classes/RDPagingView/RDPagingView.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "RDPagingView.h"
#import <objc/runtime.h>

typedef struct {
BOOL pagingViewWillChangeViewSize;
Expand All @@ -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 () <UIScrollViewDelegate>
{
NSMutableDictionary *queueDictionary_;
NSMutableSet *usingViews_;
NSMutableSet *preparedViews_;
NSInteger pageIndex_;
RDPagingViewDelegateFlag flag_;
id<UIScrollViewDelegate> delegate_;
Expand All @@ -34,7 +51,6 @@ @interface RDPagingView () <UIScrollViewDelegate>

@implementation RDPagingView

NSInteger const RDSubViewTagOffset = 3;
static NSInteger const kPreloadDefaultCount = 1;

- (instancetype)initWithFrame:(CGRect)frame
Expand All @@ -50,7 +66,6 @@ - (instancetype)initWithFrame:(CGRect)frame

queueDictionary_ = [NSMutableDictionary new];
usingViews_ = [NSMutableSet new];
preparedViews_ = [NSMutableSet new];
}

return self;
Expand Down Expand Up @@ -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];
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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];
}
}
Expand All @@ -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
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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 = ...
Expand All @@ -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

Expand Down

0 comments on commit 7e084ec

Please sign in to comment.