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 #107 from PanajotisMaroungas/master
Browse files Browse the repository at this point in the history
variable hideBars, scroll to navigate through paragraphs and scroll to read the content are added, README.md is updated
  • Loading branch information
hebertialmeida authored Aug 12, 2016
2 parents 221746d + 5920de6 commit 954d8c6
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 46 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Then, follow the steps as described in Carthage's [README](https://github.com/Ca

## Basic Usage

To get started, this is a simple usage sample.
To get started, this is a simple usage sample of using the integrated view controller.

```swift
import FolioReaderKit
Expand All @@ -74,6 +74,18 @@ func open(sender: AnyObject) {
}
```

You can also use your own FolioReader view controller like this.

```swift
let config = FolioReaderConfig()

let bookPath = NSBundle.mainBundle().pathForResource("book", ofType: "epub")
let epubVC = FolioReaderContainer(config: config, epubPath: bookPath, removeEpub: true)

// present the epubVC view controller like every other UIViewController instance
self.presentViewController(epubVC, animated: true, completion: nil)
```

In your `AppDelegate` call `applicationWillResignActive` and `applicationWillTerminate`. This will save the reader state even if you kill the app.

```swift
Expand Down Expand Up @@ -103,7 +115,7 @@ func applicationWillTerminate(application: UIApplication) {
- [x] Media Overlays (Sync text rendering with audio playback)
- [x] TTS - Text to Speech Support
- [x] Parse epub cover image
- [x] Vertical and Horizontal scrolling
- [x] Vertical or/and Horizontal scrolling
- [x] RTL Support
- [ ] PDF support
- [ ] Book Search
Expand Down
64 changes: 51 additions & 13 deletions Source/FolioReaderCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,19 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
var animator: ZFModalTransitionAnimator!
var pageIndicatorView: FolioReaderPageIndicator!
var bookShareLink: String?

var pageIndicatorHeight: CGFloat = 20

var recentlyScrolled = false
var recentlyScrolledDelay = 2.0 // 2 second delay until we clear recentlyScrolled
var recentlyScrolledTimer: NSTimer!
var scrollScrubber: ScrollScrubber!

private var screenBounds: CGRect!
private var pointNow = CGPointZero
private let pageIndicatorHeight: CGFloat = 20
private var pageOffsetRate: CGFloat = 0
private var tempReference: FRTocReference?
private var isFirstLoad = true
private var currentWebViewScrollPositions = [Int: CGPoint]()

// MARK: - View life cicle

Expand Down Expand Up @@ -87,7 +88,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
pageIndicatorView = FolioReaderPageIndicator(frame: CGRect(x: 0, y: view.frame.height-pageIndicatorHeight, width: view.frame.width, height: pageIndicatorHeight))
view.addSubview(pageIndicatorView)

let scrubberY: CGFloat = readerConfig.shouldHideNavigationOnTap == true ? 50 : 74
let scrubberY: CGFloat = ((readerConfig.shouldHideNavigationOnTap == true || readerConfig.hideBars == true) ? 50 : 74)
scrollScrubber = ScrollScrubber(frame: CGRect(x: pageWidth + 10, y: scrubberY, width: 40, height: pageHeight - 100))
scrollScrubber.delegate = self
view.addSubview(scrollScrubber.slider)
Expand Down Expand Up @@ -514,9 +515,16 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

func pagesForCurrentPage(page: FolioReaderPage?) {
guard let page = page else { return }
let pageSize = isVerticalDirection(pageHeight, pageWidth)
pageIndicatorView.totalPages = Int(ceil(page.webView.scrollView.contentSize.forDirection()/pageSize))
let webViewPage = pageForOffset(page.webView.scrollView.contentOffset.x, pageHeight: pageSize)

var pageSize = isVerticalDirection(pageHeight, pageWidth)
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)
}
pageIndicatorView.currentPage = webViewPage
}

Expand Down Expand Up @@ -873,7 +881,23 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

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)

var webViewPage = pageForOffset(page.webView.scrollView.contentOffset.forDirection(), pageHeight: pageSize)

if (readerConfig.scrollDirection == .sectionHorizontalContentVertical),
let cell = ((scrollView.superview as? UIWebView)?.delegate as? FolioReaderPage) {

let currentIndexPathRow = cell.pageNumber - 1

// if the cell reload don't save the top position offset
if let oldOffSet = self.currentWebViewScrollPositions[currentIndexPathRow]
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 {
pageIndicatorView.currentPage = webViewPage
}
Expand All @@ -885,7 +909,13 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
isScrolling = false


if (readerConfig.scrollDirection == .sectionHorizontalContentVertical),
let cell = ((scrollView.superview as? UIWebView)?.delegate as? FolioReaderPage) {
let currentIndexPathRow = cell.pageNumber - 1
self.currentWebViewScrollPositions[currentIndexPathRow] = scrollView.contentOffset
}

if scrollView is UICollectionView {
if totalPages > 0 { updateCurrentPage() }
}
Expand Down Expand Up @@ -991,11 +1021,14 @@ extension FolioReaderCenter: FolioReaderPageDelegate {
if let position = FolioReader.defaults.valueForKey(kBookId) as? NSDictionary {
let pageNumber = position["pageNumber"]! as! Int
var pageOffset: CGFloat = 0

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


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
}

if isFirstLoad {
updateCurrentPage(page)
isFirstLoad = false
Expand All @@ -1016,6 +1049,11 @@ extension FolioReaderCenter: FolioReaderPageDelegate {
currentPage.handleAnchor(fragmentID, avoidBeginningAnchors: true, animated: true)
tempFragment = nil
}

if (readerConfig.scrollDirection == .sectionHorizontalContentVertical),
let offsetPoint = self.currentWebViewScrollPositions[page.pageNumber - 1] {
page.webView.scrollView.setContentOffset(offsetPoint, animated: false)
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions Source/FolioReaderConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import UIKit
public enum FolioReaderScrollDirection: Int {
case vertical
case horizontal
case sectionHorizontalContentVertical

/**
The current scroll direction
Expand All @@ -21,7 +22,7 @@ public enum FolioReaderScrollDirection: Int {
switch self {
case vertical:
return .Vertical
case horizontal:
case horizontal, sectionHorizontalContentVertical:
return .Horizontal
}
}
Expand All @@ -41,7 +42,9 @@ public class FolioReaderConfig: NSObject {
public lazy var mediaOverlayColor: UIColor! = self.tintColor

// MARK: Custom actions

/// hide the navigation bar and the bottom status view
public var hideBars = false

/// If `canChangeScrollDirection` is `true` it will be overrided by user's option.
public var scrollDirection: FolioReaderScrollDirection = .vertical

Expand Down
31 changes: 22 additions & 9 deletions Source/FolioReaderContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var readerConfig: FolioReaderConfig!
var epubPath: String?
var book: FRBook!

class FolioReaderContainer: UIViewController {
public class FolioReaderContainer: UIViewController {
var centerNavigationController: UINavigationController!
var centerViewController: FolioReaderCenter!
var audioPlayer: FolioReaderAudioPlayer!
Expand All @@ -23,7 +23,7 @@ class FolioReaderContainer: UIViewController {

// MARK: - Init

required init?(coder aDecoder: NSCoder) {
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

Expand All @@ -50,19 +50,26 @@ class FolioReaderContainer: UIViewController {
kCurrentMediaOverlayStyle: MediaOverlayStyle.Default.rawValue,
kCurrentScrollDirection: FolioReaderScrollDirection.vertical.rawValue
])
FolioReader.sharedInstance.readerContainer = self

if (readerConfig.scrollDirection == .sectionHorizontalContentVertical) {
readerConfig.canChangeScrollDirection = false
}
}

// MARK: - View life cicle

override func viewDidLoad() {
override public func viewDidLoad() {
super.viewDidLoad()

// If user can change scroll direction use the last saved
if readerConfig.canChangeScrollDirection {
if (readerConfig.canChangeScrollDirection) {
let direction = FolioReaderScrollDirection(rawValue: FolioReader.currentScrollDirection) ?? .vertical
readerConfig.scrollDirection = direction
}


readerConfig.shouldHideNavigationOnTap = ((readerConfig.hideBars == true) ? true : readerConfig.shouldHideNavigationOnTap)

centerViewController = FolioReaderCenter()
FolioReader.sharedInstance.readerCenter = centerViewController

Expand All @@ -72,6 +79,12 @@ class FolioReaderContainer: UIViewController {
addChildViewController(centerNavigationController)
centerNavigationController.didMoveToParentViewController(self)

if (readerConfig.hideBars == true) {
readerConfig.shouldHideNavigationOnTap = false
self.navigationController?.navigationBar.hidden = true
self.centerViewController.pageIndicatorHeight = 0
}

// Read async book
if (epubPath != nil) {
let priority = DISPATCH_QUEUE_PRIORITY_HIGH
Expand Down Expand Up @@ -115,7 +128,7 @@ class FolioReaderContainer: UIViewController {
}
}

override func viewDidAppear(animated: Bool) {
override public func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

if errorOnLoad {
Expand All @@ -133,15 +146,15 @@ class FolioReaderContainer: UIViewController {

// MARK: - Status Bar

override func prefersStatusBarHidden() -> Bool {
override public func prefersStatusBarHidden() -> Bool {
return readerConfig.shouldHideNavigationOnTap == false ? false : shouldHideStatusBar
}

override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
override public func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
return UIStatusBarAnimation.Slide
}

override func preferredStatusBarStyle() -> UIStatusBarStyle {
override public func preferredStatusBarStyle() -> UIStatusBarStyle {
return isNight(.LightContent, .Default)
}
}
2 changes: 1 addition & 1 deletion Source/FolioReaderKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ enum MediaOverlayStyle: Int {
* Main Library class with some useful constants and methods
*/
public class FolioReader : NSObject {
static let sharedInstance = FolioReader()
public static let sharedInstance = FolioReader()
static let defaults = NSUserDefaults.standardUserDefaults()
weak var readerCenter: FolioReaderCenter!
weak var readerContainer: FolioReaderContainer!
Expand Down
50 changes: 31 additions & 19 deletions Source/FolioReaderPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,24 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
}

func webViewFrame() -> CGRect {
let paddingTop: CGFloat = 20

guard readerConfig.hideBars == false else { return UIScreen.mainScreen().bounds }

let paddingTop: CGFloat = 20
let paddingBottom: CGFloat = 30
guard readerConfig.shouldHideNavigationOnTap else {
let statusbarHeight = UIApplication.sharedApplication().statusBarFrame.size.height
let navBarHeight = FolioReader.sharedInstance.readerCenter.navigationController?.navigationBar.frame.size.height
let navTotal = statusbarHeight + navBarHeight!
let newFrame = CGRect(
x: bounds.origin.x,
y: isVerticalDirection(bounds.origin.y + navTotal, bounds.origin.y + navTotal + paddingTop),
width: bounds.width,
height: isVerticalDirection(bounds.height - navTotal, bounds.height - navTotal - paddingTop - paddingBottom))
return newFrame
}

guard readerConfig.shouldHideNavigationOnTap else {
let statusbarHeight = UIApplication.sharedApplication().statusBarFrame.size.height
let navBarHeight = FolioReader.sharedInstance.readerCenter.navigationController?.navigationBar.frame.size.height
let navTotal = statusbarHeight + navBarHeight!
let newFrame = CGRect(
x: bounds.origin.x,
y: isVerticalDirection(bounds.origin.y + navTotal, bounds.origin.y + navTotal + paddingTop),
width: bounds.width,
height: isVerticalDirection(bounds.height - navTotal, bounds.height - navTotal - paddingTop - paddingBottom))
return newFrame
}

let newFrame = CGRect(
x: bounds.origin.x,
y: isVerticalDirection(bounds.origin.y, bounds.origin.y + paddingTop),
Expand Down Expand Up @@ -145,7 +148,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni

let direction: ScrollDirection = FolioReader.needsRTLChange ? .positive() : .negative()

if pageScrollDirection == direction && isScrolling {
if pageScrollDirection == direction && isScrolling && readerConfig.scrollDirection != .sectionHorizontalContentVertical {
scrollPageToBottom()
}

Expand Down Expand Up @@ -298,8 +301,13 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
- parameter animated: Enable or not scrolling animation
*/
func scrollPageToOffset(offset: CGFloat, animated: Bool) {
let pageOffsetPoint = isVerticalDirection(CGPoint(x: 0, y: offset), CGPoint(x: offset, y: 0))
webView.scrollView.setContentOffset(pageOffsetPoint, animated: animated)
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)
}
}

/**
Expand Down Expand Up @@ -624,11 +632,15 @@ extension UIWebView {
paginationMode = .LeftToRight
paginationBreakingMode = .Page
scrollView.bounces = false
} else {
} else if readerConfig.scrollDirection == .vertical {
scrollView.pagingEnabled = false
paginationMode = .Unpaginated
scrollView.bounces = true
}
} else {
// swipe paragraphs horizontal, read content vertical
scrollView.bounces = true
self.scrollView.showsVerticalScrollIndicator = true
}
}
}

Expand Down

0 comments on commit 954d8c6

Please sign in to comment.