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 #84 from KyonLi/master
Browse files Browse the repository at this point in the history
WIP Horizontal scrolling
  • Loading branch information
hebertialmeida authored Jul 7, 2016
2 parents f2be587 + 07008ab commit 16eaa5f
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 42 deletions.
53 changes: 34 additions & 19 deletions Source/FolioReaderCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate {
}

func sliderChange(slider:UISlider) {
let offset = CGPointMake(0, height()*CGFloat(slider.value))
let offset = CGPointMake(height()*CGFloat(slider.value), 0)
scrollView().setContentOffset(offset, animated: false)
}

Expand Down Expand Up @@ -157,7 +157,7 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate {
}

if scrollStart == nil {
scrollStart = scrollView.contentOffset.y
scrollStart = scrollView.contentOffset.x
}
}

Expand All @@ -172,7 +172,7 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate {
show()

} else if delegate.currentPage != nil && scrollStart != nil {
scrollDelta = scrollView.contentOffset.y - scrollStart
scrollDelta = scrollView.contentOffset.x - scrollStart

if scrollDeltaTimer == nil && scrollDelta > (pageHeight * 0.2 ) || (scrollDelta * -1) > (pageHeight * 0.2) {
show()
Expand All @@ -197,7 +197,7 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate {
scrollDeltaTimer = nil
}

scrollStart = scrollView().contentOffset.y
scrollStart = scrollView().contentOffset.x
scrollDelta = 0
}

Expand All @@ -217,7 +217,7 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate {
}

private func scrollTop() -> CGFloat {
return delegate.currentPage.webView.scrollView.contentOffset.y
return delegate.currentPage.webView.scrollView.contentOffset.x
}

}
Expand Down Expand Up @@ -256,7 +256,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
layout.sectionInset = UIEdgeInsetsZero
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal

let background = isNight(readerConfig.nightModeBackground, UIColor.whiteColor())
view.backgroundColor = background
Expand All @@ -268,6 +268,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
collectionView.dataSource = self
collectionView.pagingEnabled = true
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.backgroundColor = background
collectionView.decelerationRate = UIScrollViewDecelerationRateFast
view.addSubview(collectionView)
Expand All @@ -291,7 +292,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
let scrubberY: CGFloat = readerConfig.shouldHideNavigationOnTap == true ? 50 : 74
scrollScrubber = ScrollScrubber(frame: CGRect(x: pageWidth + 10, y: scrubberY, width: 40, height: pageHeight - 100))
scrollScrubber.delegate = self
view.addSubview(scrollScrubber.slider)
// view.addSubview(scrollScrubber.slider)

// Loading indicator
let style: UIActivityIndicatorViewStyle = isNight(.White, .Gray)
Expand Down Expand Up @@ -320,6 +321,11 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
let navText = isNight(UIColor.whiteColor(), UIColor.blackColor())
let font = UIFont(name: "Avenir-Light", size: 17)!
setTranslucentNavigation(color: navBackground, tintColor: tintColor, titleColor: navText, andFont: font)
if FolioReader.sharedInstance.nightMode {
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
} else {
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.Default, animated: true)
}
}

func configureNavBarButtons() {
Expand Down Expand Up @@ -575,18 +581,18 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
if orientation.isPortrait {
if screenBounds.size.width < screenBounds.size.height {
pageWidth = screenBounds.size.width
pageHeight = screenBounds.size.height
pageHeight = screenBounds.size.height - 20 - pageIndicatorHeight
} else {
pageWidth = screenBounds.size.height
pageHeight = screenBounds.size.width
pageHeight = screenBounds.size.width - 20 - pageIndicatorHeight
}
} else {
if screenBounds.size.width > screenBounds.size.height {
pageWidth = screenBounds.size.width
pageHeight = screenBounds.size.height
pageHeight = screenBounds.size.height - 20 - pageIndicatorHeight
} else {
pageWidth = screenBounds.size.height
pageHeight = screenBounds.size.width
pageHeight = screenBounds.size.width - 20 - pageIndicatorHeight
}
}
}
Expand Down Expand Up @@ -627,17 +633,20 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

if let readingTime = page.webView.js("getReadingTime()") {
pageIndicatorView.totalMinutes = Int(readingTime)!
pagesForCurrentPage(page)

} else {
pageIndicatorView.totalMinutes = 0
}
pagesForCurrentPage(page)
}

if (completion != nil) { completion!() }
}

func pagesForCurrentPage(page: FolioReaderPage?) {
if let page = page {
pageIndicatorView.totalPages = Int(ceil(page.webView.scrollView.contentSize.height/pageHeight))
let webViewPage = pageForOffset(currentPage.webView.scrollView.contentOffset.y, pageHeight: pageHeight)
pageIndicatorView.totalPages = Int(ceil(page.webView.scrollView.contentSize.width/pageWidth))
let webViewPage = pageForOffset(currentPage.webView.scrollView.contentOffset.x, pageHeight: pageWidth)
pageIndicatorView.currentPage = webViewPage
}
}
Expand Down Expand Up @@ -677,7 +686,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
}

func frameForPage(page: Int) -> CGRect {
return CGRectMake(0, pageHeight * CGFloat(page-1), pageWidth, pageHeight)
return CGRectMake(pageWidth * CGFloat(page-1), 0, pageWidth, pageHeight)
}

func changePageWith(page page: Int, animated: Bool = false, completion: (() -> Void)? = nil) {
Expand Down Expand Up @@ -746,7 +755,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
}

UIView.animateWithDuration(animated ? 0.3 : 0, delay: 0, options: .CurveEaseInOut, animations: { () -> Void in
self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Top, animated: false)
self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
}) { (finished: Bool) -> Void in
if (completion != nil) { completion!() }
}
Expand Down Expand Up @@ -1014,6 +1023,12 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
}

scrollScrubber.scrollViewWillBeginDragging(scrollView)

// let currentPageNum = Int(pointNow.x / scrollView.bounds.size.width) + 1
// if currentPage.webView.pageCount - 1 == currentPageNum {
// currentPage.refreshPageMode()
// }
currentPage.refreshPageMode()
}

func scrollViewDidScroll(scrollView: UIScrollView) {
Expand All @@ -1026,15 +1041,15 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

// Update current reading page
if scrollView is UICollectionView {} else {
if let page = currentPage where page.webView.scrollView.contentOffset.y+pageHeight <= page.webView.scrollView.contentSize.height {
let webViewPage = pageForOffset(page.webView.scrollView.contentOffset.y, pageHeight: pageHeight)
if let page = currentPage where page.webView.scrollView.contentOffset.x+pageWidth <= page.webView.scrollView.contentSize.width {
let webViewPage = pageForOffset(page.webView.scrollView.contentOffset.x, pageHeight: pageWidth)
if pageIndicatorView.currentPage != webViewPage {
pageIndicatorView.currentPage = webViewPage
}
}
}

scrollDirection = scrollView.contentOffset.y < pointNow.y ? .Down : .Up
scrollDirection = scrollView.contentOffset.x < pointNow.x ? .Right : .Left
}

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
Expand Down
4 changes: 2 additions & 2 deletions Source/FolioReaderContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ class FolioReaderContainer: UIViewController, FolioReaderSidePanelDelegate {
// Add gestures
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(FolioReaderContainer.handleTapGesture(_:)))
tapGestureRecognizer.numberOfTapsRequired = 1
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(FolioReaderContainer.handlePanGesture(_:)))
// let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(FolioReaderContainer.handlePanGesture(_:)))
centerNavigationController.view.addGestureRecognizer(tapGestureRecognizer)
centerNavigationController.view.addGestureRecognizer(panGestureRecognizer)
// centerNavigationController.view.addGestureRecognizer(panGestureRecognizer)

// Read async book
if (epubPath != nil) {
Expand Down
9 changes: 1 addition & 8 deletions Source/FolioReaderKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,46 +70,40 @@ public class FolioReader : NSObject {
get { return FolioReader.defaults.boolForKey(kNightMode) }
set (value) {
FolioReader.defaults.setBool(value, forKey: kNightMode)
FolioReader.defaults.synchronize()
}
}
var currentFontName: Int {
get { return FolioReader.defaults.valueForKey(kCurrentFontFamily) as! Int }
set (value) {
FolioReader.defaults.setValue(value, forKey: kCurrentFontFamily)
FolioReader.defaults.synchronize()
}
}

var currentFontSize: Int {
get { return FolioReader.defaults.valueForKey(kCurrentFontSize) as! Int }
set (value) {
FolioReader.defaults.setValue(value, forKey: kCurrentFontSize)
FolioReader.defaults.synchronize()
}
}

var currentAudioRate: Int {
get { return FolioReader.defaults.valueForKey(kCurrentAudioRate) as! Int }
set (value) {
FolioReader.defaults.setValue(value, forKey: kCurrentAudioRate)
FolioReader.defaults.synchronize()
}
}

var currentHighlightStyle: Int {
get { return FolioReader.defaults.valueForKey(kCurrentHighlightStyle) as! Int }
set (value) {
FolioReader.defaults.setValue(value, forKey: kCurrentHighlightStyle)
FolioReader.defaults.synchronize()
}
}

var currentMediaOverlayStyle: MediaOverlayStyle {
get { return MediaOverlayStyle(rawValue: FolioReader.defaults.valueForKey(kCurrentMediaOverlayStyle) as! Int)! }
set (value) {
FolioReader.defaults.setValue(value.rawValue, forKey: kCurrentMediaOverlayStyle)
FolioReader.defaults.synchronize()
}
}

Expand Down Expand Up @@ -158,11 +152,10 @@ public class FolioReader : NSObject {
if let currentPage = FolioReader.sharedInstance.readerCenter.currentPage {
let position = [
"pageNumber": currentPageNumber,
"pageOffset": currentPage.webView.scrollView.contentOffset.y
"pageOffset": currentPage.webView.scrollView.contentOffset.x
]

FolioReader.defaults.setObject(position, forKey: kBookId)
FolioReader.defaults.synchronize()
}
}
}
Expand Down
Loading

0 comments on commit 16eaa5f

Please sign in to comment.