< - movePadding && _lastScrContX < 10) { _scrollView.zoomScale = _minZoomScale; [self forwardPage]; } if (scrollView.contentSize.width - scrollView.contentOffset.x < KMainW - movePadding && scrollView.contentSize.width != 0 && fabsf(_lastScrContX + KMainW - scrollView.contentSize.width) < 10) { _scrollView.zoomScale = _minZoomScale; [self nextPage]; }}//滑动自然停止时调用- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ _lastScrContX = scrollView.contentOffset.x;}//滑动手动停止时调用- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ _lastScrContX = scrollView.contentOffset.x;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
PDF文件视图:HWPDFBrowseView
#import @interface HWPDFBrowseView : UIView{ CGPDFDocumentRef pdfDocumentRef;}@property (nonatomic, assign) NSInteger currentPage;@property (nonatomic, assign) NSInteger totalPages;- (id)initWithFilePath:(NSString *)filePath;- (void)reloadView;- (void)prePage;- (void)nextPage;@end/*** ---------------分割线--------------- ***/#import "HWPDFBrowseView.h"@implementation HWPDFBrowseView- (id)initWithFilePath:(NSString *)filePath{ pdfDocumentRef = [self createPDFFromExistFile:filePath]; self = [super initWithFrame:CGPDFPageGetBoxRect(CGPDFDocumentGetPage(pdfDocumentRef, 1), kCGPDFMediaBox)]; return self;}- (CGPDFDocumentRef)createPDFFromExistFile:(NSString *)aFilePath{ CFStringRef path = CFStringCreateWithCString(NULL, [aFilePath UTF8String], kCFStringEncodingUTF8); CFURLRef urlRef = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, NO); CFRelease(path); CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(urlRef); CFRelease(urlRef); _totalPages = CGPDFDocumentGetNumberOfPages(document); _currentPage = 1; if (_totalPages == 0) return NULL; return document;}- (void)reloadView{ [self setNeedsDisplay];}- (void)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); [[UIColor whiteColor] set]; CGContextFillRect(context, rect); CGContextTranslateCTM(context, 0.0, rect.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGPDFPageRef page = CGPDFDocumentGetPage(pdfDocumentRef, _currentPage); CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, rect, 0, true); CGContextConcatCTM(context, pdfTransform); CGContextDrawPDFPage(context, page);}//上一页- (void)prePage{ if(_currentPage < 2) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已经第一页了!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil ]; [alert show]; return; } --_currentPage; [self reloadView]; [self transitionWithType:@"pageUnCurl" WithSubtype:kCATransitionFromRight ForView:self];}//下一页- (void)nextPage{ if(_currentPage >
= _ totalPages) {UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@ "prompt that" message:@ "is on the last page!" Delegate:self cancelButtonTitle:@ "OK" otherButtonTitles: nil]; [alert show]; return;} + _ currentPage; [self reloadView]; [self transitionWithType:@ "pageCurl" WithSubtype:kCATransitionFromRight ForView:self];} / / set the page flip effect-(void) transitionWithType: (NSString *) type WithSubtype: (NSString *) subtype ForView: (UIView *) view {CATransition * animation = [CATransition animation]; animation.duration = 0.7f; animation.type = type; if (subtype) animation.subtype = subtype; animation.timingFunction = UIViewAnimationOptionCurveEaseInOut [view.layer addAnimation:animation forKey:@ "animation"];} @ end
Thank you for reading this article carefully. I hope the article "iOS how to achieve PDF document browsing function" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!