Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3 from PanajotisMaroungas/javascript_webView
Browse files Browse the repository at this point in the history
refactoring & renaming isVerticalDirection to isDirection
  • Loading branch information
PanajotisMaroungas authored Aug 12, 2016
2 parents bb46894 + b49ca57 commit 2047197
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 63 deletions.
53 changes: 21 additions & 32 deletions Source/FolioReaderCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
)
}

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions Source/FolioReaderConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +31,7 @@ public enum FolioReaderScrollDirection: Int {
switch self {
case vertical:
return .Vertical
case horizontal, sectionHorizontalContentVertical:
case horizontal, horizontalWithVerticalContent:
return .Horizontal
}
}
Expand Down
4 changes: 1 addition & 3 deletions Source/FolioReaderContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 14 additions & 10 deletions Source/FolioReaderKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,51 +207,55 @@ func isNight<T> (f: T, _ l: T) -> T {

// MARK: - Scroll Direction Functions

func isVerticalDirection<T> (f: T, _ l: T) -> T {
return readerConfig.scrollDirection == .vertical ? f : l
func isDirection<T> (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)
}
}

Expand Down
24 changes: 10 additions & 14 deletions Source/FolioReaderPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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)
)

Expand Down
4 changes: 2 additions & 2 deletions Source/ScrollScrubber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 2047197

Please sign in to comment.