From b49ca57b05e85b7c2fbe9531a1c2757e675a1520 Mon Sep 17 00:00:00 2001 From: PanajotisMaroungas Date: Fri, 12 Aug 2016 17:31:47 +0200 Subject: [PATCH] refactoring & renaming isVerticalDirection to isDirection --- Source/FolioReaderCenter.swift | 53 ++++++++++++------------------- Source/FolioReaderConfig.swift | 4 +-- Source/FolioReaderContainer.swift | 4 +-- Source/FolioReaderKit.swift | 24 ++++++++------ Source/FolioReaderPage.swift | 24 ++++++-------- Source/ScrollScrubber.swift | 4 +-- 6 files changed, 50 insertions(+), 63 deletions(-) diff --git a/Source/FolioReaderCenter.swift b/Source/FolioReaderCenter.swift index 087d3bd72..d727c6879 100755 --- a/Source/FolioReaderCenter.swift +++ b/Source/FolioReaderCenter.swift @@ -223,7 +223,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio pageOffset = page * pageWidth } - let pageOffsetPoint = isVerticalDirection(CGPoint(x: 0, y: pageOffset), CGPoint(x: pageOffset, y: 0)) + let pageOffsetPoint = isDirection(CGPoint(x: 0, y: pageOffset), CGPoint(x: pageOffset, y: 0), CGPoint(x: pageOffset, y: 0)) pageScrollView.setContentOffset(pageOffsetPoint, animated: true) } } @@ -398,9 +398,10 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio self.scrollScrubber.slider.frame = scrollScrubberFrame // Adjust collectionView - self.collectionView.contentSize = isVerticalDirection( + self.collectionView.contentSize = isDirection( CGSize(width: pageWidth, height: pageHeight * CGFloat(self.totalPages)), - CGSize(width: pageWidth * CGFloat(self.totalPages), height: pageHeight) + CGSize(width: pageWidth * CGFloat(self.totalPages), height: pageHeight), + CGSize(width: pageWidth * CGFloat(self.totalPages), height: pageHeight) ) self.collectionView.setContentOffset(self.frameForPage(currentPageNumber).origin, animated: false) self.collectionView.collectionViewLayout.invalidateLayout() @@ -431,10 +432,8 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio pageOffset = page * pageWidth } - if readerConfig.scrollDirection != .sectionHorizontalContentVertical { - let pageOffsetPoint = isVerticalDirection(CGPoint(x: 0, y: pageOffset), CGPoint(x: pageOffset, y: 0)) - currentPage.webView.scrollView.setContentOffset(pageOffsetPoint, animated: true) - } + let pageOffsetPoint = isDirection(CGPoint(x: 0, y: pageOffset), CGPoint(x: pageOffset, y: 0), CGPoint(x: 0, y: pageOffset)) + currentPage.webView.scrollView.setContentOffset(pageOffsetPoint, animated: true) } override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { @@ -518,15 +517,12 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio func pagesForCurrentPage(page: FolioReaderPage?) { guard let page = page else { return } - var pageSize = isVerticalDirection(pageHeight, pageWidth) + let pageSize = isDirection(pageHeight, pageWidth, pageHeight) pageIndicatorView.totalPages = Int(ceil(page.webView.scrollView.contentSize.forDirection()/pageSize)) - var webViewPage = pageForOffset(page.webView.scrollView.contentOffset.x, pageHeight: pageSize) - if readerConfig.scrollDirection == .sectionHorizontalContentVertical { - pageSize = pageHeight - pageIndicatorView.totalPages = Int(ceil(page.webView.scrollView.contentSize.height/pageSize)) - webViewPage = pageForOffset(page.webView.scrollView.contentOffset.y, pageHeight: pageSize) - } + let pageOffSet = isDirection(page.webView.scrollView.contentOffset.x, page.webView.scrollView.contentOffset.x, page.webView.scrollView.contentOffset.y) + let webViewPage = pageForOffset(pageOffSet, pageHeight: pageSize) + pageIndicatorView.currentPage = webViewPage } @@ -565,9 +561,10 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio } func frameForPage(page: Int) -> CGRect { - return isVerticalDirection( + return isDirection( CGRectMake(0, pageHeight * CGFloat(page-1), pageWidth, pageHeight), - CGRectMake(pageWidth * CGFloat(page-1), 0, pageWidth, pageHeight) + CGRectMake(pageWidth * CGFloat(page-1), 0, pageWidth, pageHeight), + CGRectMake(0, pageHeight * CGFloat(page-1), pageWidth, pageHeight) ) } @@ -879,14 +876,14 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio // Update current reading page if scrollView is UICollectionView {} else { - let pageSize = isVerticalDirection(pageHeight, pageWidth) + let pageSize = isDirection(pageHeight, pageWidth, pageHeight) if let page = currentPage where page.webView.scrollView.contentOffset.forDirection()+pageSize <= page.webView.scrollView.contentSize.forDirection() { - var webViewPage = pageForOffset(page.webView.scrollView.contentOffset.forDirection(), pageHeight: pageSize) + let webViewPage = pageForOffset(page.webView.scrollView.contentOffset.forDirection(), pageHeight: pageSize) - if (readerConfig.scrollDirection == .sectionHorizontalContentVertical), + if (readerConfig.scrollDirection == .horizontalWithVerticalContent), let cell = ((scrollView.superview as? UIWebView)?.delegate as? FolioReaderPage) { let currentIndexPathRow = cell.pageNumber - 1 @@ -896,8 +893,6 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio where (abs(oldOffSet.y - scrollView.contentOffset.y) > 100) {} else { self.currentWebViewScrollPositions[currentIndexPathRow] = scrollView.contentOffset } - - webViewPage = pageForOffset(page.webView.scrollView.contentOffset.y, pageHeight: pageHeight) } if pageIndicatorView.currentPage != webViewPage { @@ -912,7 +907,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio func scrollViewDidEndDecelerating(scrollView: UIScrollView) { isScrolling = false - if (readerConfig.scrollDirection == .sectionHorizontalContentVertical), + if (readerConfig.scrollDirection == .horizontalWithVerticalContent), let cell = ((scrollView.superview as? UIWebView)?.delegate as? FolioReaderPage) { let currentIndexPathRow = cell.pageNumber - 1 self.currentWebViewScrollPositions[currentIndexPathRow] = scrollView.contentOffset @@ -1022,21 +1017,15 @@ extension FolioReaderCenter: FolioReaderPageDelegate { if let position = FolioReader.defaults.valueForKey(kBookId) as? NSDictionary { let pageNumber = position["pageNumber"]! as! Int - var pageOffset: CGFloat = 0 - - if readerConfig.scrollDirection == .sectionHorizontalContentVertical, - let offSetY = position["pageOffsetY"] as? CGFloat { - pageOffset = offSetY - } else if let offset = isVerticalDirection(position["pageOffsetY"], position["pageOffsetX"]) as? CGFloat { - pageOffset = offset - } + let offset = isDirection(position["pageOffsetY"], position["pageOffsetX"], position["pageOffsetY"]) as? CGFloat + let pageOffset = offset if isFirstLoad { updateCurrentPage(page) isFirstLoad = false if currentPageNumber == pageNumber && pageOffset > 0 { - page.scrollPageToOffset(pageOffset, animated: false) + page.scrollPageToOffset(pageOffset!, animated: false) } } else if !isScrolling && FolioReader.needsRTLChange { page.scrollPageToBottom() @@ -1052,7 +1041,7 @@ extension FolioReaderCenter: FolioReaderPageDelegate { tempFragment = nil } - if (readerConfig.scrollDirection == .sectionHorizontalContentVertical), + if (readerConfig.scrollDirection == .horizontalWithVerticalContent), let offsetPoint = self.currentWebViewScrollPositions[page.pageNumber - 1] { page.webView.scrollView.setContentOffset(offsetPoint, animated: false) } diff --git a/Source/FolioReaderConfig.swift b/Source/FolioReaderConfig.swift index ccc97d5a0..54fc927ba 100755 --- a/Source/FolioReaderConfig.swift +++ b/Source/FolioReaderConfig.swift @@ -20,7 +20,7 @@ public enum FolioReaderScrollDirection: Int { case horizontal /// Sections scroll horizontal and content scroll on vertical - case sectionHorizontalContentVertical + case horizontalWithVerticalContent /** The current scroll direction @@ -31,7 +31,7 @@ public enum FolioReaderScrollDirection: Int { switch self { case vertical: return .Vertical - case horizontal, sectionHorizontalContentVertical: + case horizontal, horizontalWithVerticalContent: return .Horizontal } } diff --git a/Source/FolioReaderContainer.swift b/Source/FolioReaderContainer.swift index 93b520a0d..563700ee1 100755 --- a/Source/FolioReaderContainer.swift +++ b/Source/FolioReaderContainer.swift @@ -58,9 +58,7 @@ public class FolioReaderContainer: UIViewController { ]) FolioReader.sharedInstance.readerContainer = self - if (readerConfig.scrollDirection == .sectionHorizontalContentVertical) { - readerConfig.canChangeScrollDirection = false - } + readerConfig.canChangeScrollDirection = isDirection(readerConfig.canChangeScrollDirection, readerConfig.canChangeScrollDirection, false) } required public init?(coder aDecoder: NSCoder) { diff --git a/Source/FolioReaderKit.swift b/Source/FolioReaderKit.swift index fa38cd5bb..a4a176e37 100755 --- a/Source/FolioReaderKit.swift +++ b/Source/FolioReaderKit.swift @@ -207,51 +207,55 @@ func isNight (f: T, _ l: T) -> T { // MARK: - Scroll Direction Functions -func isVerticalDirection (f: T, _ l: T) -> T { - return readerConfig.scrollDirection == .vertical ? f : l +func isDirection (vertical: T, _ horizontal: T, _ horizontalContentVertical: T) -> T { + switch readerConfig.scrollDirection { + case .vertical: return vertical + case .horizontal: return horizontal + case .horizontalWithVerticalContent: return horizontalContentVertical + } } extension UICollectionViewScrollDirection { static func direction() -> UICollectionViewScrollDirection { - return isVerticalDirection(.Vertical, .Horizontal) + return isDirection(.Vertical, .Horizontal, .Horizontal) } } extension UICollectionViewScrollPosition { static func direction() -> UICollectionViewScrollPosition { - return isVerticalDirection(.Top, .Left) + return isDirection(.Top, .Left, .Left) } } extension CGPoint { func forDirection() -> CGFloat { - return isVerticalDirection(y, x) + return isDirection(y, x, y) } } extension CGSize { func forDirection() -> CGFloat { - return isVerticalDirection(height, width) + return isDirection(height, width, height) } func forReverseDirection() -> CGFloat { - return isVerticalDirection(width, height) + return isDirection(width, height, width) } } extension CGRect { func forDirection() -> CGFloat { - return isVerticalDirection(height, width) + return isDirection(height, width, height) } } extension ScrollDirection { static func negative() -> ScrollDirection { - return isVerticalDirection(.Down, .Right) + return isDirection(.Down, .Right, .Right) } static func positive() -> ScrollDirection { - return isVerticalDirection(.Up, .Left) + return isDirection(.Up, .Left, .Left) } } diff --git a/Source/FolioReaderPage.swift b/Source/FolioReaderPage.swift index e51f0655d..564017e23 100755 --- a/Source/FolioReaderPage.swift +++ b/Source/FolioReaderPage.swift @@ -90,17 +90,17 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni let navTotal = statusbarHeight + navBarHeight! let newFrame = CGRect( x: bounds.origin.x, - y: isVerticalDirection(bounds.origin.y + navTotal, bounds.origin.y + navTotal + paddingTop), + y: isDirection(bounds.origin.y + navTotal, bounds.origin.y + navTotal + paddingTop, bounds.origin.y + navTotal + paddingTop), width: bounds.width, - height: isVerticalDirection(bounds.height - navTotal, bounds.height - navTotal - paddingTop - paddingBottom)) + height: isDirection(bounds.height - navTotal, bounds.height - navTotal - paddingTop - paddingBottom, bounds.height - navTotal - paddingTop - paddingBottom)) return newFrame } let newFrame = CGRect( x: bounds.origin.x, - y: isVerticalDirection(bounds.origin.y, bounds.origin.y + paddingTop), + y: isDirection(bounds.origin.y, bounds.origin.y + paddingTop, bounds.origin.y + paddingTop), width: bounds.width, - height: isVerticalDirection(bounds.height, bounds.height - paddingTop - paddingBottom)) + height: isDirection(bounds.height, bounds.height - paddingTop - paddingBottom, bounds.height - paddingTop - paddingBottom)) return newFrame } @@ -148,7 +148,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni let direction: ScrollDirection = FolioReader.needsRTLChange ? .positive() : .negative() - if pageScrollDirection == direction && isScrolling && readerConfig.scrollDirection != .sectionHorizontalContentVertical { + if pageScrollDirection == direction && isScrolling && readerConfig.scrollDirection != .horizontalWithVerticalContent { scrollPageToBottom() } @@ -301,21 +301,17 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni - parameter animated: Enable or not scrolling animation */ func scrollPageToOffset(offset: CGFloat, animated: Bool) { - if readerConfig.scrollDirection == .sectionHorizontalContentVertical { - let pageOffsetPoint = CGPoint(x: 0, y: offset) - webView.scrollView.setContentOffset(pageOffsetPoint, animated: animated) - } else { - let pageOffsetPoint = isVerticalDirection(CGPoint(x: 0, y: offset), CGPoint(x: offset, y: 0)) - webView.scrollView.setContentOffset(pageOffsetPoint, animated: animated) - } - } + let pageOffsetPoint = isDirection(CGPoint(x: 0, y: offset), CGPoint(x: offset, y: 0), CGPoint(x: 0, y: offset)) + webView.scrollView.setContentOffset(pageOffsetPoint, animated: animated) + } /** Scrolls the page to bottom */ func scrollPageToBottom() { - let bottomOffset = isVerticalDirection( + let bottomOffset = isDirection( CGPointMake(0, webView.scrollView.contentSize.height - webView.scrollView.bounds.height), + CGPointMake(webView.scrollView.contentSize.width - webView.scrollView.bounds.width, 0), CGPointMake(webView.scrollView.contentSize.width - webView.scrollView.bounds.width, 0) ) diff --git a/Source/ScrollScrubber.swift b/Source/ScrollScrubber.swift index 2fa662d79..beaaacb91 100644 --- a/Source/ScrollScrubber.swift +++ b/Source/ScrollScrubber.swift @@ -77,8 +77,8 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate { } func sliderChange(slider:UISlider) { - let offset = isVerticalDirection(CGPointMake(0, height()*CGFloat(slider.value)), - CGPointMake(height()*CGFloat(slider.value), 0)) + let offset = isDirection(CGPointMake(0, height()*CGFloat(slider.value)), + CGPointMake(height()*CGFloat(slider.value), 0), CGPointMake(height()*CGFloat(slider.value), 0)) scrollView().setContentOffset(offset, animated: false) }