Skip to content

Commit

Permalink
1. add code protect for not load the same pdf twice
Browse files Browse the repository at this point in the history
2. can scroll out of bounds with blank page, after stop, redraw pages
  • Loading branch information
Wonday committed Nov 22, 2017
1 parent cfa27eb commit 7843585
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 44 deletions.
30 changes: 25 additions & 5 deletions ios/RCTPdf/RCTPdfView.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ - (instancetype)initWithFrame:(CGRect)frame
self.maximumZoomScale = 3.0;
self.bouncesZoom = NO;
self.bounces = YES;
self.delaysContentTouches = NO;

// fix statusbar effect when statusbar show/hide
if (@available(iOS 11.0, *)) {
Expand Down Expand Up @@ -178,17 +177,38 @@ - (void)setOnChange:(RCTBubblingEventBlock)onChange
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.bounds.size.width>0 && self.bounds.size.height>0 && _needUpdateBounds) {
if (_needUpdateBounds) {
[wPdfView updateBounds];
_needUpdateBounds = NO;
}
}

- (void)didSetProps:(NSArray<NSString *> *)changedProps
{
[wPdfView loadPdf];
_needUpdateBounds = YES;
[self setNeedsLayout];
long int count = [changedProps count];
BOOL needLoadPdf = NO;

for (int i = 0 ; i < count; i++) {
if ([[changedProps objectAtIndex:i] isEqualToString:@"path"]
||[[changedProps objectAtIndex:i] isEqualToString:@"password"]) {
needLoadPdf = YES;
}

if ([[changedProps objectAtIndex:i] isEqualToString:@"horizontal"]
||[[changedProps objectAtIndex:i] isEqualToString:@"fitWidth"]
||[[changedProps objectAtIndex:i] isEqualToString:@"spacing"]) {
_needUpdateBounds = YES;
}
}

if (needLoadPdf) {
[wPdfView loadPdf];
_needUpdateBounds = YES;
}

if (_needUpdateBounds) {
[self setNeedsLayout];
}
}

/**
Expand Down
56 changes: 17 additions & 39 deletions ios/RCTPdf/WPdfView.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@
// output log both debug and release
#define ALog( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )

#define SCREEN_BUFFER_NUM 7.0f
#define SCREEN_BUFFER_NUM 5.0f
#define DEFAULT_SPACING 10
#define INVALID_TARGETCONTENTOFFSET -100000.0f

@implementation WPdfView
{
Expand All @@ -47,11 +46,11 @@ @implementation WPdfView
CGPoint _pageCanvasOffset;
int _lastPage;
CGFloat _pageOffset;
CGFloat _targetContentOffset;

double _numberOfBufferPages;
BOOL _isScrollToUpOrLeft; // TRUE:Up/Left FALSE:Down/Right
BOOL _needFixPageOffset;
BOOL _needUpdateBounds;

}

Expand All @@ -78,10 +77,10 @@ - (instancetype)initWithFrame:(CGRect)frame
_pdfPageRect = CGRectZero;
_pageCanvasSize = CGSizeZero;
_pageCanvasOffset = CGPointZero;
_targetContentOffset = INVALID_TARGETCONTENTOFFSET;

_isScrollToUpOrLeft = NO;
_needFixPageOffset = NO;
_needUpdateBounds = YES;

}

Expand All @@ -94,13 +93,6 @@ - (void)setPath:(NSString *)path

if (![path isEqualToString:_path]) {

if (_pdfDoc != NULL) {

CGPDFDocumentRelease(_pdfDoc);
_pdfDoc = NULL;

}

_path = [path copy];
_page = 1;
_pageOffset = 0;
Expand All @@ -119,13 +111,6 @@ - (void)setPassword:(NSString *)password

if (![password isEqualToString:_password]) {

if (_pdfDoc != NULL) {

CGPDFDocumentRelease(_pdfDoc);
_pdfDoc = NULL;

}

_password = [password copy];

if (_password == nil || _password.length == 0) {
Expand Down Expand Up @@ -189,7 +174,12 @@ - (void)setSpacing:(int)spacing
- (void) loadPdf {

// have loaded
if (_pdfDoc!=NULL) return;
if (_pdfDoc != NULL) {

CGPDFDocumentRelease(_pdfDoc);
_pdfDoc = NULL;

}

if (_path != nil && _path.length != 0) {

Expand Down Expand Up @@ -427,8 +417,8 @@ - (void)updateBounds
- (void)dealloc
{
if(_pdfDoc != NULL) {
CGPDFDocumentRelease(_pdfDoc);
_pdfDoc = NULL;
CGPDFDocumentRelease(_pdfDoc);
_pdfDoc = NULL;
}
}

Expand Down Expand Up @@ -462,7 +452,7 @@ - (void)fixPageOffset
}

((UIScrollView *)self.superview).contentOffset = contentOffset;
((UIScrollView *)self.superview).decelerationRate = UIScrollViewDecelerationRateNormal;
((UIScrollView *)self.superview).contentSize = self.bounds.size;
[self setNeedsDisplay];
}

Expand All @@ -483,7 +473,6 @@ - (void)scrollViewWillEndDragging:(CGPoint)velocity
if (_horizontal) {
if (velocity.x ==0) {

_targetContentOffset = INVALID_TARGETCONTENTOFFSET;
[self fixPageOffset];

}else if (velocity.x < 0) {
Expand All @@ -492,7 +481,7 @@ - (void)scrollViewWillEndDragging:(CGPoint)velocity
_needFixPageOffset = YES;

if (targetContentOffset->x <= 0 ) {
_targetContentOffset = targetContentOffset->x;

}

} else {
Expand All @@ -501,14 +490,13 @@ - (void)scrollViewWillEndDragging:(CGPoint)velocity
_needFixPageOffset = YES;

if (targetContentOffset->x >= contentSize.width - self.superview.bounds.size.width && _page<_numberOfPages - _numberOfBufferPages + 1) {
_targetContentOffset = targetContentOffset->x;
contentSize.width = targetContentOffset->x + 2*self.superview.bounds.size.width*((UIScrollView *)self.superview).zoomScale;
}

}
} else {
if (velocity.y == 0) {

_targetContentOffset = INVALID_TARGETCONTENTOFFSET;
[self fixPageOffset];

}else if (velocity.y < 0) {
Expand All @@ -517,7 +505,7 @@ - (void)scrollViewWillEndDragging:(CGPoint)velocity
_needFixPageOffset = YES;

if (targetContentOffset->y <= 0 ) {
_targetContentOffset = targetContentOffset->y;

}

} else {
Expand All @@ -526,27 +514,17 @@ - (void)scrollViewWillEndDragging:(CGPoint)velocity
_needFixPageOffset = YES;

if (targetContentOffset->y >= contentSize.height - self.superview.bounds.size.height && _page<_numberOfPages - _numberOfBufferPages + 1) {
_targetContentOffset = targetContentOffset->y;
contentSize.height = targetContentOffset->y + 2*self.superview.bounds.size.height*((UIScrollView *)self.superview).zoomScale;
}

}
}

((UIScrollView *)self.superview).contentSize = contentSize;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (_horizontal) {
if (_targetContentOffset!=INVALID_TARGETCONTENTOFFSET && fabs(_targetContentOffset-((UIScrollView *)self.superview).contentOffset.x)<=100) {
((UIScrollView *)self.superview).decelerationRate = UIScrollViewDecelerationRateFast;
_targetContentOffset = INVALID_TARGETCONTENTOFFSET;
}
} else {
if (_targetContentOffset!=INVALID_TARGETCONTENTOFFSET && fabs(_targetContentOffset-((UIScrollView *)self.superview).contentOffset.y)<=100) {
((UIScrollView *)self.superview).decelerationRate = UIScrollViewDecelerationRateFast;
_targetContentOffset = INVALID_TARGETCONTENTOFFSET;
}
}
[self noticePageChanged];
}

Expand Down

0 comments on commit 7843585

Please sign in to comment.