ReaderViewController.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. //
  2. // ReaderViewController.m
  3. // Reader v2.8.6
  4. //
  5. // Created by Julius Oklamcak on 2011-07-01.
  6. // Copyright © 2011-2015 Julius Oklamcak. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights to
  11. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. // of the Software, and to permit persons to whom the Software is furnished to
  13. // do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. #import "ReaderConstants.h"
  26. #import "ReaderViewController.h"
  27. #import "ThumbsViewController.h"
  28. #import "ReaderMainToolbar.h"
  29. #import "ReaderMainPagebar.h"
  30. #import "ReaderContentView.h"
  31. #import "ReaderThumbCache.h"
  32. #import "ReaderThumbQueue.h"
  33. #import <MessageUI/MessageUI.h>
  34. @interface ReaderViewController () <UIScrollViewDelegate, UIGestureRecognizerDelegate, MFMailComposeViewControllerDelegate, UIDocumentInteractionControllerDelegate,
  35. ReaderMainToolbarDelegate, ReaderMainPagebarDelegate, ReaderContentViewDelegate, ThumbsViewControllerDelegate>
  36. @end
  37. @implementation ReaderViewController
  38. {
  39. ReaderDocument *document;
  40. UIScrollView *theScrollView;
  41. ReaderMainToolbar *mainToolbar;
  42. ReaderMainPagebar *mainPagebar;
  43. NSMutableDictionary *contentViews;
  44. UIUserInterfaceIdiom userInterfaceIdiom;
  45. NSInteger currentPage, minimumPage, maximumPage;
  46. UIDocumentInteractionController *documentInteraction;
  47. UIPrintInteractionController *printInteraction;
  48. CGFloat scrollViewOutset;
  49. CGSize lastAppearSize;
  50. NSDate *lastHideTime;
  51. BOOL ignoreDidScroll;
  52. }
  53. #pragma mark - Constants
  54. #define STATUS_HEIGHT 20.0f
  55. #define TOOLBAR_HEIGHT 44.0f
  56. #define PAGEBAR_HEIGHT 48.0f
  57. #define SCROLLVIEW_OUTSET_SMALL 4.0f
  58. #define SCROLLVIEW_OUTSET_LARGE 8.0f
  59. #define TAP_AREA_SIZE 48.0f
  60. #pragma mark - Properties
  61. @synthesize delegate;
  62. #pragma mark - ReaderViewController methods
  63. - (void)updateContentSize:(UIScrollView *)scrollView
  64. {
  65. CGFloat contentHeight = scrollView.bounds.size.height; // Height
  66. CGFloat contentWidth = (scrollView.bounds.size.width * maximumPage);
  67. scrollView.contentSize = CGSizeMake(contentWidth, contentHeight);
  68. }
  69. - (void)updateContentViews:(UIScrollView *)scrollView
  70. {
  71. [self updateContentSize:scrollView]; // Update content size first
  72. [contentViews enumerateKeysAndObjectsUsingBlock: // Enumerate content views
  73. ^(NSNumber *key, ReaderContentView *contentView, BOOL *stop)
  74. {
  75. NSInteger page = [key integerValue]; // Page number value
  76. CGRect viewRect = CGRectZero; viewRect.size = scrollView.bounds.size;
  77. viewRect.origin.x = (viewRect.size.width * (page - 1)); // Update X
  78. contentView.frame = CGRectInset(viewRect, scrollViewOutset, 0.0f);
  79. }
  80. ];
  81. NSInteger page = currentPage; // Update scroll view offset to current page
  82. CGPoint contentOffset = CGPointMake((scrollView.bounds.size.width * (page - 1)), 0.0f);
  83. if (CGPointEqualToPoint(scrollView.contentOffset, contentOffset) == false) // Update
  84. {
  85. scrollView.contentOffset = contentOffset; // Update content offset
  86. }
  87. [mainToolbar setBookmarkState:[document.bookmarks containsIndex:page]];
  88. [mainPagebar updatePagebar]; // Update page bar
  89. }
  90. - (void)addContentView:(UIScrollView *)scrollView page:(NSInteger)page
  91. {
  92. CGRect viewRect = CGRectZero; viewRect.size = scrollView.bounds.size;
  93. viewRect.origin.x = (viewRect.size.width * (page - 1)); viewRect = CGRectInset(viewRect, scrollViewOutset, 0.0f);
  94. NSURL *fileURL = document.fileURL; NSString *phrase = document.password; NSString *guid = document.guid; // Document properties
  95. ReaderContentView *contentView = [[ReaderContentView alloc] initWithFrame:viewRect fileURL:fileURL page:page password:phrase]; // ReaderContentView
  96. contentView.message = self; [contentViews setObject:contentView forKey:[NSNumber numberWithInteger:page]]; [scrollView addSubview:contentView];
  97. [contentView showPageThumb:fileURL page:page password:phrase guid:guid]; // Request page preview thumb
  98. }
  99. - (void)layoutContentViews:(UIScrollView *)scrollView
  100. {
  101. CGFloat viewWidth = scrollView.bounds.size.width; // View width
  102. CGFloat contentOffsetX = scrollView.contentOffset.x; // Content offset X
  103. NSInteger pageB = ((contentOffsetX + viewWidth - 1.0f) / viewWidth); // Pages
  104. NSInteger pageA = (contentOffsetX / viewWidth); pageB += 2; // Add extra pages
  105. if (pageA < minimumPage) pageA = minimumPage; if (pageB > maximumPage) pageB = maximumPage;
  106. NSRange pageRange = NSMakeRange(pageA, (pageB - pageA + 1)); // Make page range (A to B)
  107. NSMutableIndexSet *pageSet = [NSMutableIndexSet indexSetWithIndexesInRange:pageRange];
  108. for (NSNumber *key in [contentViews allKeys]) // Enumerate content views
  109. {
  110. NSInteger page = [key integerValue]; // Page number value
  111. if ([pageSet containsIndex:page] == NO) // Remove content view
  112. {
  113. ReaderContentView *contentView = [contentViews objectForKey:key];
  114. [contentView removeFromSuperview]; [contentViews removeObjectForKey:key];
  115. }
  116. else // Visible content view - so remove it from page set
  117. {
  118. [pageSet removeIndex:page];
  119. }
  120. }
  121. NSInteger pages = pageSet.count;
  122. if (pages > 0) // We have pages to add
  123. {
  124. NSEnumerationOptions options = 0; // Default
  125. if (pages == 2) // Handle case of only two content views
  126. {
  127. if ((maximumPage > 2) && ([pageSet lastIndex] == maximumPage)) options = NSEnumerationReverse;
  128. }
  129. else if (pages == 3) // Handle three content views - show the middle one first
  130. {
  131. NSMutableIndexSet *workSet = [pageSet mutableCopy]; options = NSEnumerationReverse;
  132. [workSet removeIndex:[pageSet firstIndex]]; [workSet removeIndex:[pageSet lastIndex]];
  133. NSInteger page = [workSet firstIndex]; [pageSet removeIndex:page];
  134. [self addContentView:scrollView page:page];
  135. }
  136. [pageSet enumerateIndexesWithOptions:options usingBlock: // Enumerate page set
  137. ^(NSUInteger page, BOOL *stop)
  138. {
  139. [self addContentView:scrollView page:page];
  140. }
  141. ];
  142. }
  143. }
  144. - (void)handleScrollViewDidEnd:(UIScrollView *)scrollView
  145. {
  146. CGFloat viewWidth = scrollView.bounds.size.width; // Scroll view width
  147. CGFloat contentOffsetX = scrollView.contentOffset.x; // Content offset X
  148. NSInteger page = (contentOffsetX / viewWidth); page++; // Page number
  149. if (page != currentPage) // Only if on different page
  150. {
  151. currentPage = page; document.pageNumber = [NSNumber numberWithInteger:page];
  152. [contentViews enumerateKeysAndObjectsUsingBlock: // Enumerate content views
  153. ^(NSNumber *key, ReaderContentView *contentView, BOOL *stop)
  154. {
  155. if ([key integerValue] != page) [contentView zoomResetAnimated:NO];
  156. }
  157. ];
  158. [mainToolbar setBookmarkState:[document.bookmarks containsIndex:page]];
  159. [mainPagebar updatePagebar]; // Update page bar
  160. }
  161. }
  162. - (void)showDocumentPage:(NSInteger)page
  163. {
  164. if (page != currentPage) // Only if on different page
  165. {
  166. if ((page < minimumPage) || (page > maximumPage)) return;
  167. currentPage = page; document.pageNumber = [NSNumber numberWithInteger:page];
  168. CGPoint contentOffset = CGPointMake((theScrollView.bounds.size.width * (page - 1)), 0.0f);
  169. if (CGPointEqualToPoint(theScrollView.contentOffset, contentOffset) == true)
  170. [self layoutContentViews:theScrollView];
  171. else
  172. [theScrollView setContentOffset:contentOffset];
  173. [contentViews enumerateKeysAndObjectsUsingBlock: // Enumerate content views
  174. ^(NSNumber *key, ReaderContentView *contentView, BOOL *stop)
  175. {
  176. if ([key integerValue] != page) [contentView zoomResetAnimated:NO];
  177. }
  178. ];
  179. [mainToolbar setBookmarkState:[document.bookmarks containsIndex:page]];
  180. [mainPagebar updatePagebar]; // Update page bar
  181. }
  182. }
  183. - (void)showDocument
  184. {
  185. [self updateContentSize:theScrollView]; // Update content size first
  186. [self showDocumentPage:[document.pageNumber integerValue]]; // Show page
  187. document.lastOpen = [NSDate date]; // Update document last opened date
  188. }
  189. - (void)closeDocument
  190. {
  191. if (printInteraction != nil) [printInteraction dismissAnimated:NO];
  192. [document archiveDocumentProperties]; // Save any ReaderDocument changes
  193. [[ReaderThumbQueue sharedInstance] cancelOperationsWithGUID:document.guid];
  194. [[ReaderThumbCache sharedInstance] removeAllObjects]; // Empty the thumb cache
  195. if ([delegate respondsToSelector:@selector(dismissReaderViewController:)] == YES)
  196. {
  197. [delegate dismissReaderViewController:self]; // Dismiss the ReaderViewController
  198. }
  199. else // We have a "Delegate must respond to -dismissReaderViewController:" error
  200. {
  201. NSAssert(NO, @"Delegate must respond to -dismissReaderViewController:");
  202. }
  203. }
  204. #pragma mark - UIViewController methods
  205. - (instancetype)initWithReaderDocument:(ReaderDocument *)object
  206. {
  207. if ((self = [super initWithNibName:nil bundle:nil])) // Initialize superclass
  208. {
  209. if ((object != nil) && ([object isKindOfClass:[ReaderDocument class]])) // Valid object
  210. {
  211. userInterfaceIdiom = [UIDevice currentDevice].userInterfaceIdiom; // User interface idiom
  212. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; // Default notification center
  213. [notificationCenter addObserver:self selector:@selector(applicationWillResign:) name:UIApplicationWillTerminateNotification object:nil];
  214. [notificationCenter addObserver:self selector:@selector(applicationWillResign:) name:UIApplicationWillResignActiveNotification object:nil];
  215. scrollViewOutset = ((userInterfaceIdiom == UIUserInterfaceIdiomPad) ? SCROLLVIEW_OUTSET_LARGE : SCROLLVIEW_OUTSET_SMALL);
  216. [object updateDocumentProperties]; document = object; // Retain the supplied ReaderDocument object for our use
  217. [ReaderThumbCache touchThumbCacheWithGUID:object.guid]; // Touch the document thumb cache directory
  218. }
  219. else // Invalid ReaderDocument object
  220. {
  221. self = nil;
  222. }
  223. }
  224. return self;
  225. }
  226. - (void)dealloc
  227. {
  228. [[NSNotificationCenter defaultCenter] removeObserver:self];
  229. }
  230. - (void)viewDidLoad
  231. {
  232. [super viewDidLoad];
  233. assert(document != nil); // Must have a valid ReaderDocument
  234. self.view.backgroundColor = [UIColor grayColor]; // Neutral gray
  235. UIView *fakeStatusBar = nil; CGRect viewRect = self.view.bounds; // View bounds
  236. if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) // iOS 7+
  237. {
  238. if ([self prefersStatusBarHidden] == NO) // Visible status bar
  239. {
  240. CGRect statusBarRect = viewRect; statusBarRect.size.height = STATUS_HEIGHT;
  241. fakeStatusBar = [[UIView alloc] initWithFrame:statusBarRect]; // UIView
  242. fakeStatusBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  243. fakeStatusBar.backgroundColor = [UIColor blackColor];
  244. fakeStatusBar.contentMode = UIViewContentModeRedraw;
  245. fakeStatusBar.userInteractionEnabled = NO;
  246. viewRect.origin.y += STATUS_HEIGHT; viewRect.size.height -= STATUS_HEIGHT;
  247. }
  248. }
  249. CGRect scrollViewRect = CGRectInset(viewRect, -scrollViewOutset, 0.0f);
  250. theScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect]; // All
  251. theScrollView.autoresizesSubviews = NO; theScrollView.contentMode = UIViewContentModeRedraw;
  252. theScrollView.showsHorizontalScrollIndicator = NO; theScrollView.showsVerticalScrollIndicator = NO;
  253. theScrollView.scrollsToTop = NO; theScrollView.delaysContentTouches = NO; theScrollView.pagingEnabled = YES;
  254. theScrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
  255. theScrollView.backgroundColor = [UIColor clearColor]; theScrollView.delegate = self;
  256. [self.view addSubview:theScrollView];
  257. CGRect toolbarRect = viewRect; toolbarRect.size.height = TOOLBAR_HEIGHT;
  258. mainToolbar = [[ReaderMainToolbar alloc] initWithFrame:toolbarRect document:document]; // ReaderMainToolbar
  259. mainToolbar.delegate = self; // ReaderMainToolbarDelegate
  260. //TWS [self.view addSubview:mainToolbar];
  261. CGRect pagebarRect = self.view.bounds; pagebarRect.size.height = PAGEBAR_HEIGHT;
  262. pagebarRect.origin.y = (self.view.bounds.size.height - pagebarRect.size.height);
  263. mainPagebar = [[ReaderMainPagebar alloc] initWithFrame:pagebarRect document:document]; // ReaderMainPagebar
  264. mainPagebar.delegate = self; // ReaderMainPagebarDelegate
  265. [self.view addSubview:mainPagebar];
  266. if (fakeStatusBar != nil) [self.view addSubview:fakeStatusBar]; // Add status bar background view
  267. UITapGestureRecognizer *singleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
  268. singleTapOne.numberOfTouchesRequired = 1; singleTapOne.numberOfTapsRequired = 1; singleTapOne.delegate = self;
  269. [self.view addGestureRecognizer:singleTapOne];
  270. UITapGestureRecognizer *doubleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
  271. doubleTapOne.numberOfTouchesRequired = 1; doubleTapOne.numberOfTapsRequired = 2; doubleTapOne.delegate = self;
  272. [self.view addGestureRecognizer:doubleTapOne];
  273. UITapGestureRecognizer *doubleTapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
  274. doubleTapTwo.numberOfTouchesRequired = 2; doubleTapTwo.numberOfTapsRequired = 2; doubleTapTwo.delegate = self;
  275. [self.view addGestureRecognizer:doubleTapTwo];
  276. UISwipeGestureRecognizer *swipeUpDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpDown:)];
  277. swipeUpDown.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
  278. [self.view addGestureRecognizer:swipeUpDown];
  279. // TWS Long Press
  280. UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongTap:)];
  281. [self.view addGestureRecognizer:longPressRecognizer];
  282. [singleTapOne requireGestureRecognizerToFail:doubleTapOne]; // Single tap requires double tap to fail
  283. contentViews = [NSMutableDictionary new]; lastHideTime = [NSDate date];
  284. minimumPage = 1; maximumPage = [document.pageCount integerValue];
  285. }
  286. - (void)viewWillAppear:(BOOL)animated
  287. {
  288. [super viewWillAppear:animated];
  289. if (CGSizeEqualToSize(lastAppearSize, CGSizeZero) == false)
  290. {
  291. if (CGSizeEqualToSize(lastAppearSize, self.view.bounds.size) == false)
  292. {
  293. [self updateContentViews:theScrollView]; // Update content views
  294. }
  295. lastAppearSize = CGSizeZero; // Reset view size tracking
  296. }
  297. }
  298. - (void)viewDidAppear:(BOOL)animated
  299. {
  300. [super viewDidAppear:animated];
  301. if (CGSizeEqualToSize(theScrollView.contentSize, CGSizeZero) == true)
  302. {
  303. [self performSelector:@selector(showDocument) withObject:nil afterDelay:0.0];
  304. }
  305. #if (READER_DISABLE_IDLE == TRUE) // Option
  306. [UIApplication sharedApplication].idleTimerDisabled = YES;
  307. #endif // end of READER_DISABLE_IDLE Option
  308. }
  309. - (void)viewWillDisappear:(BOOL)animated
  310. {
  311. [super viewWillDisappear:animated];
  312. lastAppearSize = self.view.bounds.size; // Track view size
  313. #if (READER_DISABLE_IDLE == TRUE) // Option
  314. [UIApplication sharedApplication].idleTimerDisabled = NO;
  315. #endif // end of READER_DISABLE_IDLE Option
  316. }
  317. - (void)viewDidDisappear:(BOOL)animated
  318. {
  319. [super viewDidDisappear:animated];
  320. }
  321. - (void)viewDidUnload
  322. {
  323. #ifdef DEBUG
  324. NSLog(@"%s", __FUNCTION__);
  325. #endif
  326. mainToolbar = nil; mainPagebar = nil;
  327. theScrollView = nil; contentViews = nil; lastHideTime = nil;
  328. documentInteraction = nil; printInteraction = nil;
  329. lastAppearSize = CGSizeZero; currentPage = 0;
  330. [super viewDidUnload];
  331. }
  332. - (BOOL)prefersStatusBarHidden
  333. {
  334. return YES;
  335. }
  336. - (UIStatusBarStyle)preferredStatusBarStyle
  337. {
  338. return UIStatusBarStyleLightContent;
  339. }
  340. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  341. {
  342. return YES;
  343. }
  344. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  345. {
  346. if (userInterfaceIdiom == UIUserInterfaceIdiomPad) if (printInteraction != nil) [printInteraction dismissAnimated:NO];
  347. ignoreDidScroll = YES;
  348. }
  349. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
  350. {
  351. if (CGSizeEqualToSize(theScrollView.contentSize, CGSizeZero) == false)
  352. {
  353. [self updateContentViews:theScrollView]; lastAppearSize = CGSizeZero;
  354. }
  355. }
  356. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  357. {
  358. ignoreDidScroll = NO;
  359. }
  360. //TWS
  361. - (void)updateContentViews
  362. {
  363. if (currentPage > 0) {
  364. [self updateContentViews:theScrollView];
  365. }
  366. }
  367. - (void)didReceiveMemoryWarning
  368. {
  369. #ifdef DEBUG
  370. NSLog(@"%s", __FUNCTION__);
  371. #endif
  372. [super didReceiveMemoryWarning];
  373. }
  374. #pragma mark - UIScrollViewDelegate methods
  375. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  376. {
  377. if (ignoreDidScroll == NO) [self layoutContentViews:scrollView];
  378. }
  379. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  380. {
  381. [self handleScrollViewDidEnd:scrollView];
  382. }
  383. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
  384. {
  385. [self handleScrollViewDidEnd:scrollView];
  386. }
  387. #pragma mark - UIGestureRecognizerDelegate methods
  388. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch
  389. {
  390. if ([touch.view isKindOfClass:[UIScrollView class]]) return YES;
  391. return NO;
  392. }
  393. #pragma mark - UIGestureRecognizer action methods
  394. - (void)decrementPageNumber
  395. {
  396. if ((maximumPage > minimumPage) && (currentPage != minimumPage))
  397. {
  398. CGPoint contentOffset = theScrollView.contentOffset; // Offset
  399. contentOffset.x -= theScrollView.bounds.size.width; // View X--
  400. [theScrollView setContentOffset:contentOffset animated:YES];
  401. }
  402. }
  403. - (void)incrementPageNumber
  404. {
  405. if ((maximumPage > minimumPage) && (currentPage != maximumPage))
  406. {
  407. CGPoint contentOffset = theScrollView.contentOffset; // Offset
  408. contentOffset.x += theScrollView.bounds.size.width; // View X++
  409. [theScrollView setContentOffset:contentOffset animated:YES];
  410. }
  411. }
  412. - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
  413. {
  414. if (recognizer.state == UIGestureRecognizerStateRecognized)
  415. {
  416. //TWS
  417. if ([self.delegate respondsToSelector:@selector(handleSingleTapReader)])
  418. [self.delegate handleSingleTapReader];
  419. CGRect viewRect = recognizer.view.bounds; // View bounds
  420. CGPoint point = [recognizer locationInView:recognizer.view]; // Point
  421. CGRect areaRect = CGRectInset(viewRect, TAP_AREA_SIZE, 0.0f); // Area rect
  422. if (CGRectContainsPoint(areaRect, point) == true) // Single tap is inside area
  423. {
  424. NSNumber *key = [NSNumber numberWithInteger:currentPage]; // Page number key
  425. ReaderContentView *targetView = [contentViews objectForKey:key]; // View
  426. id target = [targetView processSingleTap:recognizer]; // Target object
  427. if (target != nil) // Handle the returned target object
  428. {
  429. if ([target isKindOfClass:[NSURL class]]) // Open a URL
  430. {
  431. NSURL *url = (NSURL *)target; // Cast to a NSURL object
  432. if (url.scheme == nil) // Handle a missing URL scheme
  433. {
  434. NSString *www = url.absoluteString; // Get URL string
  435. if ([www hasPrefix:@"www"] == YES) // Check for 'www' prefix
  436. {
  437. NSString *http = [[NSString alloc] initWithFormat:@"http://%@", www];
  438. url = [NSURL URLWithString:http]; // Proper http-based URL
  439. }
  440. }
  441. if ([[UIApplication sharedApplication] openURL:url] == NO)
  442. {
  443. #ifdef DEBUG
  444. NSLog(@"%s '%@'", __FUNCTION__, url); // Bad or unknown URL
  445. #endif
  446. }
  447. }
  448. else // Not a URL, so check for another possible object type
  449. {
  450. if ([target isKindOfClass:[NSNumber class]]) // Goto page
  451. {
  452. NSInteger number = [target integerValue]; // Number
  453. [self showDocumentPage:number]; // Show the page
  454. }
  455. }
  456. }
  457. else // Nothing active tapped in the target content view
  458. {
  459. if ([lastHideTime timeIntervalSinceNow] < -0.75) // Delay since hide
  460. {
  461. if ((mainToolbar.alpha < 1.0f) || (mainPagebar.alpha < 1.0f)) // Hidden
  462. {
  463. [mainToolbar showToolbar]; [mainPagebar showPagebar]; // Show
  464. }
  465. }
  466. }
  467. return;
  468. }
  469. CGRect nextPageRect = viewRect;
  470. nextPageRect.size.width = TAP_AREA_SIZE;
  471. nextPageRect.origin.x = (viewRect.size.width - TAP_AREA_SIZE);
  472. if (CGRectContainsPoint(nextPageRect, point) == true) // page++
  473. {
  474. [self incrementPageNumber]; return;
  475. }
  476. CGRect prevPageRect = viewRect;
  477. prevPageRect.size.width = TAP_AREA_SIZE;
  478. if (CGRectContainsPoint(prevPageRect, point) == true) // page--
  479. {
  480. [self decrementPageNumber]; return;
  481. }
  482. }
  483. }
  484. - (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer
  485. {
  486. if (recognizer.state == UIGestureRecognizerStateRecognized)
  487. {
  488. CGRect viewRect = recognizer.view.bounds; // View bounds
  489. CGPoint point = [recognizer locationInView:recognizer.view]; // Point
  490. CGRect zoomArea = CGRectInset(viewRect, TAP_AREA_SIZE, TAP_AREA_SIZE); // Area
  491. if (CGRectContainsPoint(zoomArea, point) == true) // Double tap is inside zoom area
  492. {
  493. NSNumber *key = [NSNumber numberWithInteger:currentPage]; // Page number key
  494. ReaderContentView *targetView = [contentViews objectForKey:key]; // View
  495. switch (recognizer.numberOfTouchesRequired) // Touches count
  496. {
  497. case 1: // One finger double tap: zoom++
  498. {
  499. [targetView zoomIncrement:recognizer]; break;
  500. }
  501. case 2: // Two finger double tap: zoom--
  502. {
  503. [targetView zoomDecrement:recognizer]; break;
  504. }
  505. }
  506. return;
  507. }
  508. CGRect nextPageRect = viewRect;
  509. nextPageRect.size.width = TAP_AREA_SIZE;
  510. nextPageRect.origin.x = (viewRect.size.width - TAP_AREA_SIZE);
  511. if (CGRectContainsPoint(nextPageRect, point) == true) // page++
  512. {
  513. [self incrementPageNumber]; return;
  514. }
  515. CGRect prevPageRect = viewRect;
  516. prevPageRect.size.width = TAP_AREA_SIZE;
  517. if (CGRectContainsPoint(prevPageRect, point) == true) // page--
  518. {
  519. [self decrementPageNumber]; return;
  520. }
  521. }
  522. }
  523. - (void)handleLongTap:(UITapGestureRecognizer *)recognizer
  524. {
  525. //TWS
  526. if ([self.delegate respondsToSelector:@selector(handleLongTappReader)])
  527. [self.delegate handleLongTappReader];
  528. }
  529. - (void)handleSwipeUpDown:(UITapGestureRecognizer *)recognizer
  530. {
  531. //TWS
  532. if ([self.delegate respondsToSelector:@selector(handleSwipeUpDown)])
  533. [self.delegate handleSwipeUpDown];
  534. }
  535. #pragma mark - ReaderContentViewDelegate methods
  536. - (void)contentView:(ReaderContentView *)contentView touchesBegan:(NSSet *)touches
  537. {
  538. if ((mainToolbar.alpha > 0.0f) || (mainPagebar.alpha > 0.0f))
  539. {
  540. if (touches.count == 1) // Single touches only
  541. {
  542. UITouch *touch = [touches anyObject]; // Touch info
  543. CGPoint point = [touch locationInView:self.view]; // Touch location
  544. CGRect areaRect = CGRectInset(self.view.bounds, TAP_AREA_SIZE, TAP_AREA_SIZE);
  545. if (CGRectContainsPoint(areaRect, point) == false) return;
  546. }
  547. [mainToolbar hideToolbar]; [mainPagebar hidePagebar]; // Hide
  548. lastHideTime = [NSDate date]; // Set last hide time
  549. }
  550. }
  551. #pragma mark - ReaderMainToolbarDelegate methods
  552. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar doneButton:(UIButton *)button
  553. {
  554. #if (READER_STANDALONE == FALSE) // Option
  555. [self closeDocument]; // Close ReaderViewController
  556. #endif // end of READER_STANDALONE Option
  557. }
  558. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar thumbsButton:(UIButton *)button
  559. {
  560. #if (READER_ENABLE_THUMBS == TRUE) // Option
  561. //TWS
  562. [[UIApplication sharedApplication] setStatusBarHidden:YES];
  563. if (printInteraction != nil) [printInteraction dismissAnimated:NO];
  564. ThumbsViewController *thumbsViewController = [[ThumbsViewController alloc] initWithReaderDocument:document];
  565. thumbsViewController.title = self.title; thumbsViewController.delegate = self; // ThumbsViewControllerDelegate
  566. thumbsViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  567. thumbsViewController.modalPresentationStyle = UIModalPresentationFullScreen;
  568. [self presentViewController:thumbsViewController animated:NO completion:NULL];
  569. #endif // end of READER_ENABLE_THUMBS Option
  570. }
  571. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar exportButton:(UIButton *)button
  572. {
  573. if (printInteraction != nil) [printInteraction dismissAnimated:YES];
  574. NSURL *fileURL = document.fileURL; // Document file URL
  575. documentInteraction = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
  576. documentInteraction.delegate = self; // UIDocumentInteractionControllerDelegate
  577. [documentInteraction presentOpenInMenuFromRect:button.bounds inView:button animated:YES];
  578. }
  579. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar printButton:(UIButton *)button
  580. {
  581. if ([UIPrintInteractionController isPrintingAvailable] == YES)
  582. {
  583. NSURL *fileURL = document.fileURL; // Document file URL
  584. if ([UIPrintInteractionController canPrintURL:fileURL] == YES)
  585. {
  586. printInteraction = [UIPrintInteractionController sharedPrintController];
  587. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  588. printInfo.duplex = UIPrintInfoDuplexLongEdge;
  589. printInfo.outputType = UIPrintInfoOutputGeneral;
  590. printInfo.jobName = document.fileName;
  591. printInteraction.printInfo = printInfo;
  592. printInteraction.printingItem = fileURL;
  593. printInteraction.showsPageRange = YES;
  594. if (userInterfaceIdiom == UIUserInterfaceIdiomPad) // Large device printing
  595. {
  596. [printInteraction presentFromRect:button.bounds inView:button animated:YES completionHandler:
  597. ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
  598. {
  599. #ifdef DEBUG
  600. if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
  601. #endif
  602. }
  603. ];
  604. }
  605. else // Handle printing on small device
  606. {
  607. [printInteraction presentAnimated:YES completionHandler:
  608. ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
  609. {
  610. #ifdef DEBUG
  611. if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
  612. #endif
  613. }
  614. ];
  615. }
  616. }
  617. }
  618. }
  619. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar emailButton:(UIButton *)button
  620. {
  621. if ([MFMailComposeViewController canSendMail] == NO) return;
  622. if (printInteraction != nil) [printInteraction dismissAnimated:YES];
  623. unsigned long long fileSize = [document.fileSize unsignedLongLongValue];
  624. if (fileSize < 15728640ull) // Check attachment size limit (15MB)
  625. {
  626. NSURL *fileURL = document.fileURL; NSString *fileName = document.fileName;
  627. NSData *attachment = [NSData dataWithContentsOfURL:fileURL options:(NSDataReadingMapped|NSDataReadingUncached) error:nil];
  628. if (attachment != nil) // Ensure that we have valid document file attachment data available
  629. {
  630. MFMailComposeViewController *mailComposer = [MFMailComposeViewController new];
  631. [mailComposer addAttachmentData:attachment mimeType:@"application/pdf" fileName:fileName];
  632. [mailComposer setSubject:fileName]; // Use the document file name for the subject
  633. mailComposer.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  634. mailComposer.modalPresentationStyle = UIModalPresentationFormSheet;
  635. mailComposer.mailComposeDelegate = self; // MFMailComposeViewControllerDelegate
  636. [self presentViewController:mailComposer animated:YES completion:NULL];
  637. }
  638. }
  639. }
  640. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar markButton:(UIButton *)button
  641. {
  642. #if (READER_BOOKMARKS == TRUE) // Option
  643. if (printInteraction != nil) [printInteraction dismissAnimated:YES];
  644. if ([document.bookmarks containsIndex:currentPage]) // Remove bookmark
  645. {
  646. [document.bookmarks removeIndex:currentPage]; [mainToolbar setBookmarkState:NO];
  647. }
  648. else // Add the bookmarked page number to the bookmark index set
  649. {
  650. [document.bookmarks addIndex:currentPage]; [mainToolbar setBookmarkState:YES];
  651. }
  652. #endif // end of READER_BOOKMARKS Option
  653. }
  654. #pragma mark - MFMailComposeViewControllerDelegate methods
  655. - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
  656. {
  657. #ifdef DEBUG
  658. if ((result == MFMailComposeResultFailed) && (error != NULL)) NSLog(@"%@", error);
  659. #endif
  660. [self dismissViewControllerAnimated:YES completion:NULL];
  661. }
  662. #pragma mark - UIDocumentInteractionControllerDelegate methods
  663. - (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
  664. {
  665. documentInteraction = nil;
  666. }
  667. #pragma mark - ThumbsViewControllerDelegate methods
  668. - (void)thumbsViewController:(ThumbsViewController *)viewController gotoPage:(NSInteger)page
  669. {
  670. #if (READER_ENABLE_THUMBS == TRUE) // Option
  671. [self showDocumentPage:page];
  672. #endif // end of READER_ENABLE_THUMBS Option
  673. }
  674. - (void)dismissThumbsViewController:(ThumbsViewController *)viewController
  675. {
  676. #if (READER_ENABLE_THUMBS == TRUE) // Option
  677. [self dismissViewControllerAnimated:NO completion:NULL];
  678. #endif // end of READER_ENABLE_THUMBS Option
  679. }
  680. #pragma mark - ReaderMainPagebarDelegate methods
  681. - (void)pagebar:(ReaderMainPagebar *)pagebar gotoPage:(NSInteger)page
  682. {
  683. [self showDocumentPage:page];
  684. }
  685. #pragma mark - UIApplication notification methods
  686. - (void)applicationWillResign:(NSNotification *)notification
  687. {
  688. [document archiveDocumentProperties]; // Save any ReaderDocument changes
  689. if (userInterfaceIdiom == UIUserInterfaceIdiomPad) if (printInteraction != nil) [printInteraction dismissAnimated:NO];
  690. }
  691. @end