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 #128 from smartmobilefactory/feature/ScrollToCusto…
Browse files Browse the repository at this point in the history
…mPlaces

Make the changePage and scrollTo functionality accesable to the public
  • Loading branch information
hebertialmeida authored Sep 6, 2016
2 parents b76be7a + 4eea438 commit cc28afa
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 95 deletions.
37 changes: 23 additions & 14 deletions Source/FolioReaderCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
var pages: [String]!
var totalPages: Int!
var tempFragment: String?
var currentPage: FolioReaderPage?
public private(set) var currentPage: FolioReaderPage?
var animator: ZFModalTransitionAnimator!
var pageIndicatorView: FolioReaderPageIndicator?
var pageIndicatorHeight: CGFloat = 20
Expand Down Expand Up @@ -613,17 +613,6 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
)
}

func changePageWith(page page: Int, animated: Bool = false, completion: (() -> Void)? = nil) {
if page > 0 && page-1 < totalPages {
let indexPath = NSIndexPath(forRow: page-1, inSection: 0)
changePageWith(indexPath: indexPath, animated: animated, completion: { () -> Void in
self.updateCurrentPage({ () -> Void in
completion?()
})
})
}
}

func changePageWith(page page: Int, andFragment fragment: String, animated: Bool = false, completion: (() -> Void)? = nil) {
if currentPageNumber == page {
if let currentPage = currentPage where fragment != "" {
Expand All @@ -640,7 +629,7 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
}
}

func changePageWith(href href: String, animated: Bool = false, completion: (() -> Void)? = nil) {
func changePageWith(href href: String, animated: Bool = false, completion: (() -> Void)? = nil) {
let item = findPageByHref(href)
let indexPath = NSIndexPath(forRow: item, inSection: 0)
changePageWith(indexPath: indexPath, animated: animated, completion: { () -> Void in
Expand Down Expand Up @@ -681,7 +670,7 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
completion?()
}
}

func indexPathIsValid(indexPath: NSIndexPath) -> Bool {
let section = indexPath.section
let row = indexPath.row
Expand Down Expand Up @@ -772,6 +761,26 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
}
return nil
}

// MARK: Public page methods

/**
Changes the current page of the reader.

- parameter page: The target page index. Note: The page index starts at 1 (and not 0).
- parameter animated: En-/Disables the animation of the page change.
- parameter completion: A Closure which is called if the page change is completed.
*/
public func changePageWith(page page: Int, animated: Bool = false, completion: (() -> Void)? = nil) {
if page > 0 && page-1 < totalPages {
let indexPath = NSIndexPath(forRow: page-1, inSection: 0)
changePageWith(indexPath: indexPath, animated: animated, completion: { () -> Void in
self.updateCurrentPage({ () -> Void in
completion?()
})
})
}
}

// MARK: - Audio Playing

Expand Down
2 changes: 1 addition & 1 deletion Source/FolioReaderContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var book: FRBook!
/// Reader container
public class FolioReaderContainer: UIViewController {
var centerNavigationController: UINavigationController!
var centerViewController: FolioReaderCenter!
var centerViewController: FolioReaderCenter!
var audioPlayer: FolioReaderAudioPlayer!
var shouldHideStatusBar = true
var shouldRemoveEpub = true
Expand Down
162 changes: 82 additions & 80 deletions Source/FolioReaderPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protocol FolioReaderPageDelegate: class {
func pageDidLoad(page: FolioReaderPage)
}

class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecognizerDelegate {
public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecognizerDelegate {

weak var delegate: FolioReaderPageDelegate?
var pageNumber: Int!
Expand All @@ -31,7 +31,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni

// MARK: - View life cicle

override init(frame: CGRect) {
override public init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()

Expand Down Expand Up @@ -62,15 +62,15 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
webView.addGestureRecognizer(tapGestureRecognizer)
}

required init?(coder aDecoder: NSCoder) {
required public init?(coder aDecoder: NSCoder) {
fatalError("storyboards are incompatible with truth and beauty")
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

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

webView.setupScrollDirection()
Expand Down Expand Up @@ -127,7 +127,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni

// MARK: - UIWebView Delegate

func webViewDidFinishLoad(webView: UIWebView) {
public func webViewDidFinishLoad(webView: UIWebView) {
refreshPageMode()

if readerConfig.enableTTS && !book.hasAudio() {
Expand All @@ -152,7 +152,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
delegate?.pageDidLoad(self)
}

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
public func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

guard let url = request.URL else { return false }

Expand Down Expand Up @@ -241,7 +241,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni

// MARK: Gesture recognizer

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {

if gestureRecognizer.view is UIWebView {
if otherGestureRecognizer is UILongPressGestureRecognizer {
Expand All @@ -255,7 +255,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
return false
}

func handleTapGesture(recognizer: UITapGestureRecognizer) {
public func handleTapGesture(recognizer: UITapGestureRecognizer) {
// webView.setMenuVisible(false)

if FolioReader.sharedInstance.readerCenter.navigationController!.navigationBarHidden {
Expand Down Expand Up @@ -283,82 +283,84 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
// Reset menu
menuIsVisible = false
}
// MARK: - Scroll and positioning
/**
Scrolls the page to a given offset
- parameter offset: The offset to scroll
- parameter animated: Enable or not scrolling animation
*/
func scrollPageToOffset(offset: CGFloat, animated: Bool) {
let pageOffsetPoint = isDirection(CGPoint(x: 0, y: offset), CGPoint(x: offset, y: 0))

// MARK: - Public scroll postion setter

/**
Scrolls the page to a given offset

- parameter offset: The offset to scroll
- parameter animated: Enable or not scrolling animation
*/
public func scrollPageToOffset(offset: CGFloat, animated: Bool) {
let pageOffsetPoint = isDirection(CGPoint(x: 0, y: offset), CGPoint(x: offset, y: 0))
webView.scrollView.setContentOffset(pageOffsetPoint, animated: animated)
}

/**
Scrolls the page to bottom
*/
func scrollPageToBottom() {
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)
)

if bottomOffset.forDirection() >= 0 {
dispatch_async(dispatch_get_main_queue(), {
self.webView.scrollView.setContentOffset(bottomOffset, animated: false)
})
}
}

/**
Handdle #anchors in html, get the offset and scroll to it

- parameter anchor: The #anchor
- parameter avoidBeginningAnchors: Sometimes the anchor is on the beggining of the text, there is not need to scroll
- parameter animated: Enable or not scrolling animation
*/
func handleAnchor(anchor: String, avoidBeginningAnchors: Bool, animated: Bool) {
if !anchor.isEmpty {
let offset = getAnchorOffset(anchor)

if readerConfig.scrollDirection == .vertical {
let isBeginning = offset < frame.forDirection()/2

if !avoidBeginningAnchors {
scrollPageToOffset(offset, animated: animated)
} else if avoidBeginningAnchors && !isBeginning {
scrollPageToOffset(offset, animated: animated)
}
} else {
scrollPageToOffset(offset, animated: animated)
}
}
}

/**
Get the #anchor offset in the page

- parameter anchor: The #anchor id
- returns: The element offset ready to scroll
*/
func getAnchorOffset(anchor: String) -> CGFloat {
let horizontal = readerConfig.scrollDirection == .horizontal
if let strOffset = webView.js("getAnchorOffset('\(anchor)', \(horizontal.description))") {
return CGFloat((strOffset as NSString).floatValue)
}

return CGFloat(0)
}


/**
Scrolls the page to bottom
*/
public func scrollPageToBottom() {
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)
)

if bottomOffset.forDirection() >= 0 {
dispatch_async(dispatch_get_main_queue(), {
self.webView.scrollView.setContentOffset(bottomOffset, animated: false)
})
}
}

/**
Handdle #anchors in html, get the offset and scroll to it

- parameter anchor: The #anchor
- parameter avoidBeginningAnchors: Sometimes the anchor is on the beggining of the text, there is not need to scroll
- parameter animated: Enable or not scrolling animation
*/
public func handleAnchor(anchor: String, avoidBeginningAnchors: Bool, animated: Bool) {
if !anchor.isEmpty {
let offset = getAnchorOffset(anchor)

if readerConfig.scrollDirection == .vertical {
let isBeginning = offset < frame.forDirection()/2

if !avoidBeginningAnchors {
scrollPageToOffset(offset, animated: animated)
} else if avoidBeginningAnchors && !isBeginning {
scrollPageToOffset(offset, animated: animated)
}
} else {
scrollPageToOffset(offset, animated: animated)
}
}
}

// MARK: Helper

/**
Get the #anchor offset in the page

- parameter anchor: The #anchor id
- returns: The element offset ready to scroll
*/
func getAnchorOffset(anchor: String) -> CGFloat {
let horizontal = readerConfig.scrollDirection == .horizontal
if let strOffset = webView.js("getAnchorOffset('\(anchor)', \(horizontal.description))") {
return CGFloat((strOffset as NSString).floatValue)
}

return CGFloat(0)
}

// MARK: Mark ID

/**
Audio Mark ID - marks an element with an ID with the given class and scrolls to it

- parameter ID: The ID
*/
func audioMarkID(ID: String) {
Expand All @@ -368,7 +370,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni

// MARK: UIMenu visibility

override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
override public func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if UIMenuController.sharedMenuController().menuItems?.count == 0 {
webView.isColors = false
webView.createMenu(options: false)
Expand Down

0 comments on commit cc28afa

Please sign in to comment.