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 #91 from FolioReader/horizontal-scroll
Browse files Browse the repository at this point in the history
Closes #22 Horizontal scroll
  • Loading branch information
hebertialmeida authored Jul 11, 2016
2 parents f2be587 + af62dcf commit d73b0a6
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 63 deletions.
36 changes: 18 additions & 18 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@
isa = PBXNativeTarget;
buildConfigurationList = 1A42C29B1C0E3883000F2137 /* Build configuration list for PBXNativeTarget "Example" */;
buildPhases = (
BFF78AC0EC7FAA2ADF7765C4 /* 📦 Check Pods Manifest.lock */,
BFF78AC0EC7FAA2ADF7765C4 /* [CP] Check Pods Manifest.lock */,
1A42C2851C0E3882000F2137 /* Sources */,
1A42C2861C0E3882000F2137 /* Frameworks */,
1A42C2871C0E3882000F2137 /* Resources */,
B6A166DC30D64CDC32736E22 /* 📦 Embed Pods Frameworks */,
8F716768EB59E9B6D88C48D7 /* 📦 Copy Pods Resources */,
B6A166DC30D64CDC32736E22 /* [CP] Embed Pods Frameworks */,
8F716768EB59E9B6D88C48D7 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand All @@ -178,12 +178,12 @@
isa = PBXNativeTarget;
buildConfigurationList = CA10C1381C572A4B0049165D /* Build configuration list for PBXNativeTarget "FolioReaderTests" */;
buildPhases = (
1D8126CC73301F2413345444 /* 📦 Check Pods Manifest.lock */,
1D8126CC73301F2413345444 /* [CP] Check Pods Manifest.lock */,
CA10C12D1C572A4B0049165D /* Sources */,
CA10C12E1C572A4B0049165D /* Frameworks */,
CA10C12F1C572A4B0049165D /* Resources */,
C1392FF51E4FD15EBB5AA8DD /* 📦 Embed Pods Frameworks */,
652A7706A17894A2B0BA1503 /* 📦 Copy Pods Resources */,
C1392FF51E4FD15EBB5AA8DD /* [CP] Embed Pods Frameworks */,
652A7706A17894A2B0BA1503 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -263,89 +263,89 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
1D8126CC73301F2413345444 /* 📦 Check Pods Manifest.lock */ = {
1D8126CC73301F2413345444 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Check Pods Manifest.lock";
name = "[CP] Check Pods Manifest.lock";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
652A7706A17894A2B0BA1503 /* 📦 Copy Pods Resources */ = {
652A7706A17894A2B0BA1503 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Copy Pods Resources";
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-FolioReaderTests/Pods-FolioReaderTests-resources.sh\"\n";
showEnvVarsInLog = 0;
};
8F716768EB59E9B6D88C48D7 /* 📦 Copy Pods Resources */ = {
8F716768EB59E9B6D88C48D7 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Copy Pods Resources";
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Example/Pods-Example-resources.sh\"\n";
showEnvVarsInLog = 0;
};
B6A166DC30D64CDC32736E22 /* 📦 Embed Pods Frameworks */ = {
B6A166DC30D64CDC32736E22 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Embed Pods Frameworks";
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Example/Pods-Example-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
BFF78AC0EC7FAA2ADF7765C4 /* 📦 Check Pods Manifest.lock */ = {
BFF78AC0EC7FAA2ADF7765C4 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Check Pods Manifest.lock";
name = "[CP] Check Pods Manifest.lock";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
C1392FF51E4FD15EBB5AA8DD /* 📦 Embed Pods Frameworks */ = {
C1392FF51E4FD15EBB5AA8DD /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Embed Pods Frameworks";
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
1 change: 1 addition & 0 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ViewController: UIViewController {
func openEpub(sampleNum:Int) {
let config = FolioReaderConfig()
config.shouldHideNavigationOnTap = sampleNum == 1 ? true : false
config.scrollDirection = sampleNum == 1 ? .horizontal : .vertical

// See more at FolioReaderConfig.swift
// config.enableTTS = false
Expand Down
2 changes: 1 addition & 1 deletion Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 7bafcb0eb37bf78bf161a6ebcb80be176777e3a8

COCOAPODS: 1.0.0
COCOAPODS: 1.0.1
85 changes: 67 additions & 18 deletions Source/FolioReaderCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate {
}

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

Expand Down Expand Up @@ -157,11 +158,12 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate {
}

if scrollStart == nil {
scrollStart = scrollView.contentOffset.y
scrollStart = scrollView.contentOffset.forDirection()
}
}

func scrollViewDidScroll(scrollView: UIScrollView) {
guard readerConfig.scrollDirection == .vertical else { return }

if visible && usingSlider == false {
setSliderVal()
Expand All @@ -172,7 +174,7 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate {
show()

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

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

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

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

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

}
Expand All @@ -241,7 +243,8 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

private var screenBounds: CGRect!
private var pointNow = CGPointZero
private let pageIndicatorHeight = 20 as CGFloat
private let pageIndicatorHeight: CGFloat = 20
private var pageOffsetRate: CGFloat = 0

// MARK: - View life cicle

Expand All @@ -256,7 +259,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
layout.sectionInset = UIEdgeInsetsZero
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
layout.scrollDirection = .direction()

let background = isNight(readerConfig.nightModeBackground, UIColor.whiteColor())
view.backgroundColor = background
Expand All @@ -268,6 +271,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 Down Expand Up @@ -320,6 +324,12 @@ 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 @@ -540,13 +550,20 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
self.pageIndicatorView.frame = pageIndicatorFrame
self.pageIndicatorView.reloadView(updateShadow: true)

// adjust scroll scrubber slider
// Adjust scroll scrubber slider
self.scrollScrubber.slider.frame = scrollScrubberFrame

// Adjust collectionView
self.collectionView.contentSize = CGSizeMake(pageWidth, pageHeight * CGFloat(self.totalPages))
self.collectionView.contentSize = isVerticalDirection(
CGSize(width: pageWidth, height: pageHeight * CGFloat(self.totalPages)),
CGSize(width: pageWidth * CGFloat(self.totalPages), height: pageHeight)
)
self.collectionView.setContentOffset(self.frameForPage(currentPageNumber).origin, animated: false)
self.collectionView.collectionViewLayout.invalidateLayout()

// Adjust internal page offset
let pageScrollView = self.currentPage.webView.scrollView
self.pageOffsetRate = pageScrollView.contentOffset.forDirection() / pageScrollView.contentSize.forDirection()
})
}

Expand All @@ -557,6 +574,18 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
pagesForCurrentPage(currentPage)

scrollScrubber.setSliderVal()

// After rotation fix internal page offset
var pageOffset = self.currentPage.webView.scrollView.contentSize.forDirection() * pageOffsetRate

// Fix the offset for paged scroll
if readerConfig.scrollDirection == .horizontal {
var page = round(pageOffset / pageWidth)
pageOffset = page * pageWidth
}

let pageOffsetPoint = isVerticalDirection(CGPoint(x: 0, y: pageOffset), CGPoint(x: pageOffset, y: 0))
self.currentPage.webView.scrollView.setContentOffset(pageOffsetPoint, animated: true)
}

override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
Expand Down Expand Up @@ -627,17 +656,21 @@ 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)
let pageSize = isVerticalDirection(pageHeight, pageWidth)
pageIndicatorView.totalPages = Int(ceil(page.webView.scrollView.contentSize.forDirection()/pageSize))
let webViewPage = pageForOffset(currentPage.webView.scrollView.contentOffset.x, pageHeight: pageSize)
pageIndicatorView.currentPage = webViewPage
}
}
Expand Down Expand Up @@ -677,7 +710,10 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
}

func frameForPage(page: Int) -> CGRect {
return CGRectMake(0, pageHeight * CGFloat(page-1), pageWidth, pageHeight)
return isVerticalDirection(
CGRectMake(0, pageHeight * CGFloat(page-1), pageWidth, pageHeight),
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 +782,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: .direction(), animated: false)
}) { (finished: Bool) -> Void in
if (completion != nil) { completion!() }
}
Expand Down Expand Up @@ -977,7 +1013,11 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

if let position = FolioReader.defaults.valueForKey(kBookId) as? NSDictionary {
let pageNumber = position["pageNumber"]! as! Int
let pageOffset = position["pageOffset"]! as! CGFloat
var pageOffset: CGFloat = 0

if let offset = isVerticalDirection(position["pageOffsetY"], position["pageOffsetX"]) as? CGFloat {
pageOffset = offset
}

if isFirstLoad {
updateCurrentPage(page)
Expand Down Expand Up @@ -1014,6 +1054,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 +1072,18 @@ 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)
let pageSize = isVerticalDirection(pageHeight, pageWidth)

if let page = currentPage
where page.webView.scrollView.contentOffset.forDirection()+pageSize <= page.webView.scrollView.contentSize.forDirection() {
let webViewPage = pageForOffset(page.webView.scrollView.contentOffset.forDirection(), pageHeight: pageSize)
if pageIndicatorView.currentPage != webViewPage {
pageIndicatorView.currentPage = webViewPage
}
}
}

scrollDirection = scrollView.contentOffset.y < pointNow.y ? .Down : .Up
scrollDirection = scrollView.contentOffset.forDirection() < pointNow.forDirection() ? .negative() : .positive()
}

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
Expand Down
Loading

0 comments on commit d73b0a6

Please sign in to comment.