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 #102 from FolioReader/change-scroll-from-menu-#90
Browse files Browse the repository at this point in the history
Change scroll from menu #90
  • Loading branch information
hebertialmeida authored Jul 28, 2016
2 parents 1d24122 + 621bbb7 commit 8f31fb4
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 42 deletions.
1 change: 1 addition & 0 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ViewController: UIViewController {
config.scrollDirection = sampleNum == 1 ? .horizontal : .vertical

// See more at FolioReaderConfig.swift
// config.canChangeScrollDirection = false
// config.enableTTS = false
// config.allowSharing = false
// config.tintColor = UIColor.blueColor()
Expand Down
49 changes: 41 additions & 8 deletions Source/FolioReaderCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var isScrolling = false
class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

var collectionView: UICollectionView!
let collectionViewLayout = UICollectionViewFlowLayout()
var loadingView: UIActivityIndicatorView!
var pages: [String]!
var totalPages: Int!
Expand Down Expand Up @@ -52,17 +53,16 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
setPageSize(UIApplication.sharedApplication().statusBarOrientation)

// Layout
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsetsZero
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.scrollDirection = .direction()
collectionViewLayout.sectionInset = UIEdgeInsetsZero
collectionViewLayout.minimumLineSpacing = 0
collectionViewLayout.minimumInteritemSpacing = 0
collectionViewLayout.scrollDirection = .direction()

let background = isNight(readerConfig.nightModeBackground, UIColor.whiteColor())
view.backgroundColor = background

// CollectionView
collectionView = UICollectionView(frame: screenBounds, collectionViewLayout: layout)
collectionView = UICollectionView(frame: screenBounds, collectionViewLayout: collectionViewLayout)
collectionView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
collectionView.delegate = self
collectionView.dataSource = self
Expand Down Expand Up @@ -101,7 +101,6 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
view.addSubview(loadingView)
}


override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

Expand Down Expand Up @@ -168,8 +167,40 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
currentPageNumber = 1
}

// MARK: Status bar and Navigation bar
// MARK: Change layout orientation

func setScrollDirection(direction: FolioReaderScrollDirection) {
// Get internal page offset before layout change
let pageScrollView = currentPage.webView.scrollView
pageOffsetRate = pageScrollView.contentOffset.forDirection() / pageScrollView.contentSize.forDirection()

// Change layout
readerConfig.scrollDirection = direction
collectionViewLayout.scrollDirection = .direction()
currentPage.setNeedsLayout()
collectionView.collectionViewLayout.invalidateLayout()
collectionView.setContentOffset(frameForPage(currentPageNumber).origin, animated: false)

/**
* This delay is needed because the page will not be ready yet
* so the delay wait until layout finished the changes.
*/
delay(0.1) {
var pageOffset = pageScrollView.contentSize.forDirection() * self.pageOffsetRate

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

let pageOffsetPoint = isVerticalDirection(CGPoint(x: 0, y: pageOffset), CGPoint(x: pageOffset, y: 0))
pageScrollView.setContentOffset(pageOffsetPoint, animated: true)
}
}

// MARK: Status bar and Navigation bar

func hideBars() {

if readerConfig.shouldHideNavigationOnTap == false { return }
Expand Down Expand Up @@ -230,6 +261,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

cell.pageNumber = indexPath.row+1
cell.webView.scrollView.delegate = self
cell.webView.setupScrollDirection()
cell.delegate = self
cell.backgroundColor = UIColor.clearColor()

Expand Down Expand Up @@ -353,6 +385,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

// Update pages
pagesForCurrentPage(currentPage)
currentPage.refreshPageMode()

scrollScrubber.setSliderVal()

Expand Down
22 changes: 19 additions & 3 deletions Source/FolioReaderConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public enum FolioReaderScrollDirection: Int {
}

public class FolioReaderConfig: NSObject {
// Colors

// MARK: Colors

public var tintColor = UIColor(rgba: "#6ACC50")
public var menuBackgroundColor = UIColor.whiteColor()
public var menuSeparatorColor = UIColor(rgba: "#D7D7D7")
Expand All @@ -38,13 +40,25 @@ public class FolioReaderConfig: NSObject {
public var nightModeSeparatorColor = UIColor(white: 0.5, alpha: 0.2)
public lazy var mediaOverlayColor: UIColor! = self.tintColor

// Custom actions
// MARK: Custom actions

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

/// Enable or disable hability to user change scroll direction on menu.
public var canChangeScrollDirection = true

/// Should hide navigation bar on user tap
public var shouldHideNavigationOnTap = true

/// Allow sharing option, if `false` will hide all sharing icons and options
public var allowSharing = true

/// Enable TTS (Text To Speech)
public var enableTTS = true

// Licalized strings
// MARK: Localized strings

public var localizedHighlightsTitle = NSLocalizedString("Highlights", comment: "")
public var localizedHighlightsDateFormat = "MMM dd, YYYY | HH:mm"
public var localizedHighlightMenu = NSLocalizedString("Highlight", comment: "")
Expand All @@ -54,6 +68,8 @@ public class FolioReaderConfig: NSObject {
public var localizedFontMenuNight = NSLocalizedString("Night", comment: "")
public var localizedPlayerMenuStyle = NSLocalizedString("Style", comment: "")
public var localizedFontMenuDay = NSLocalizedString("Day", comment: "")
public var localizedLayoutHorizontal = NSLocalizedString("Horizontal", comment: "")
public var localizedLayoutVertical = NSLocalizedString("Vertical", comment: "")
public var localizedReaderOnePageLeft = NSLocalizedString("1 page left", comment: "")
public var localizedReaderManyPagesLeft = NSLocalizedString("pages left", comment: "")
public var localizedReaderManyMinutes = NSLocalizedString("minutes", comment: "")
Expand Down
9 changes: 8 additions & 1 deletion Source/FolioReaderContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class FolioReaderContainer: UIViewController {
kCurrentAudioRate: 1,
kCurrentHighlightStyle: 0,
kCurrentTOCMenu: 0,
kCurrentMediaOverlayStyle: MediaOverlayStyle.Default.rawValue
kCurrentMediaOverlayStyle: MediaOverlayStyle.Default.rawValue,
kCurrentScrollDirection: FolioReaderScrollDirection.vertical.rawValue
])
}

Expand All @@ -56,6 +57,12 @@ class FolioReaderContainer: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

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

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

Expand Down
68 changes: 50 additions & 18 deletions Source/FolioReaderFontsMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRe
view.addGestureRecognizer(tapGesture)

// Menu view
menuView = UIView(frame: CGRectMake(0, view.frame.height-170, view.frame.width, view.frame.height))
let visibleHeight: CGFloat = readerConfig.canChangeScrollDirection ? 222 : 170
menuView = UIView(frame: CGRectMake(0, view.frame.height-visibleHeight, view.frame.width, view.frame.height))
menuView.backgroundColor = isNight(readerConfig.nightModeMenuBackground, UIColor.whiteColor())
menuView.autoresizingMask = .FlexibleWidth
menuView.layer.shadowColor = UIColor.blackColor().CGColor
Expand Down Expand Up @@ -125,9 +126,9 @@ class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRe
slider.addTarget(self, action: #selector(FolioReaderFontsMenu.sliderValueChanged(_:)), forControlEvents: UIControlEvents.ValueChanged)

// Force remove fill color
for layer in slider.layer.sublayers! {
slider.layer.sublayers?.forEach({ layer in
layer.backgroundColor = UIColor.clearColor().CGColor
}
})

menuView.addSubview(slider)

Expand All @@ -142,22 +143,39 @@ class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRe
fontBigView.contentMode = UIViewContentMode.Center
menuView.addSubview(fontBigView)

// // Separator 3
// let line3 = UIView(frame: CGRectMake(0, line2.frame.origin.y+56, view.frame.width, 1))
// line3.backgroundColor = readerConfig.nightModeSeparatorColor
// menuView.addSubview(line3)
// Only continues if user can change scroll direction
guard readerConfig.canChangeScrollDirection else { return }

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Status Bar

override func prefersStatusBarHidden() -> Bool {
return readerConfig.shouldHideNavigationOnTap == true
// Separator 3
let line3 = UIView(frame: CGRectMake(0, line2.frame.origin.y+56, view.frame.width, 1))
line3.backgroundColor = readerConfig.nightModeSeparatorColor
menuView.addSubview(line3)

let vertical = UIImage(readerImageNamed: "icon-menu-vertical")
let horizontal = UIImage(readerImageNamed: "icon-menu-horizontal")
let verticalNormal = vertical!.imageTintColor(normalColor).imageWithRenderingMode(.AlwaysOriginal)
let horizontalNormal = horizontal!.imageTintColor(normalColor).imageWithRenderingMode(.AlwaysOriginal)
let verticalSelected = vertical!.imageTintColor(selectedColor).imageWithRenderingMode(.AlwaysOriginal)
let horizontalSelected = horizontal!.imageTintColor(selectedColor).imageWithRenderingMode(.AlwaysOriginal)

// Layout direction
let layoutDirection = SMSegmentView(frame: CGRect(x: 0, y: line3.frame.origin.y, width: view.frame.width, height: 55),
separatorColour: readerConfig.nightModeSeparatorColor,
separatorWidth: 1,
segmentProperties: [
keySegmentTitleFont: UIFont(name: "Avenir-Light", size: 17)!,
keySegmentOnSelectionColour: UIColor.clearColor(),
keySegmentOffSelectionColour: UIColor.clearColor(),
keySegmentOnSelectionTextColour: selectedColor,
keySegmentOffSelectionTextColour: normalColor,
keyContentVerticalMargin: 17
])
layoutDirection.delegate = self
layoutDirection.tag = 3
layoutDirection.addSegmentWithTitle(readerConfig.localizedLayoutVertical, onSelectionImage: verticalSelected, offSelectionImage: verticalNormal)
layoutDirection.addSegmentWithTitle(readerConfig.localizedLayoutHorizontal, onSelectionImage: horizontalSelected, offSelectionImage: horizontalNormal)
layoutDirection.selectSegmentAtIndex(Int(FolioReader.sharedInstance.currentScrollDirection))
menuView.addSubview(layoutDirection)
}

// MARK: - SMSegmentView delegate
Expand Down Expand Up @@ -217,6 +235,14 @@ class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRe

FolioReader.sharedInstance.currentFontName = index
}

if segmentView.tag == 3 {
guard FolioReader.sharedInstance.currentScrollDirection != index else { return }

let direction = FolioReaderScrollDirection(rawValue: index) ?? .vertical
FolioReader.sharedInstance.readerCenter.setScrollDirection(direction)
FolioReader.sharedInstance.currentScrollDirection = index
}
}

// MARK: - Font slider changed
Expand Down Expand Up @@ -264,4 +290,10 @@ class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRe
}
return false
}

// MARK: - Status Bar

override func prefersStatusBarHidden() -> Bool {
return readerConfig.shouldHideNavigationOnTap == true
}
}
36 changes: 33 additions & 3 deletions Source/FolioReaderKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal let kCurrentFontSize = "com.folioreader.kCurrentFontSize"
internal let kCurrentAudioRate = "com.folioreader.kCurrentAudioRate"
internal let kCurrentHighlightStyle = "com.folioreader.kCurrentHighlightStyle"
internal var kCurrentMediaOverlayStyle = "com.folioreader.kMediaOverlayStyle"
internal var kCurrentScrollDirection = "com.folioreader.kCurrentScrollDirection"
internal let kNightMode = "com.folioreader.kNightMode"
internal let kCurrentTOCMenu = "com.folioreader.kCurrentTOCMenu"
internal let kMigratedToRealm = "com.folioreader.kMigratedToRealm"
Expand Down Expand Up @@ -109,6 +110,13 @@ public class FolioReader : NSObject {
}
}

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

// MARK: - Get Cover Image

/**
Expand Down Expand Up @@ -198,19 +206,23 @@ extension UICollectionViewScrollPosition {

extension CGPoint {
func forDirection() -> CGFloat {
return isVerticalDirection(self.y, self.x)
return isVerticalDirection(y, x)
}
}

extension CGSize {
func forDirection() -> CGFloat {
return isVerticalDirection(self.height, self.width)
return isVerticalDirection(height, width)
}

func forReverseDirection() -> CGFloat {
return isVerticalDirection(width, height)
}
}

extension CGRect {
func forDirection() -> CGFloat {
return isVerticalDirection(self.height, self.width)
return isVerticalDirection(height, width)
}
}

Expand All @@ -224,6 +236,24 @@ extension ScrollDirection {
}
}

// MARK: Helpers

/**
Delay function
From: http://stackoverflow.com/a/24318861/517707

- parameter delay: Delay in seconds
- parameter closure: Closure
*/
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}


// MARK: - Extensions

Expand Down
25 changes: 16 additions & 9 deletions Source/FolioReaderPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
}
webView.delegate = self

if readerConfig.scrollDirection == .horizontal {
webView.scrollView.pagingEnabled = true
webView.paginationMode = .LeftToRight
webView.paginationBreakingMode = .Page
webView.scrollView.bounces = false
} else {
webView.scrollView.bounces = true
}

if colorView == nil {
colorView = UIView()
colorView.backgroundColor = readerConfig.nightModeBackground
Expand All @@ -83,6 +74,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
super.layoutSubviews()

webView.frame = webViewFrame()
webView.setupScrollDirection()
}

func webViewFrame() -> CGRect {
Expand Down Expand Up @@ -610,6 +602,21 @@ extension UIWebView {
if callback!.isEmpty { return nil }
return callback
}

// MARK: WebView direction config

func setupScrollDirection() {
if readerConfig.scrollDirection == .horizontal {
self.scrollView.pagingEnabled = true
self.paginationMode = .LeftToRight
self.paginationBreakingMode = .Page
self.scrollView.bounces = false
} else {
self.scrollView.pagingEnabled = false
self.paginationMode = .Unpaginated
self.scrollView.bounces = true
}
}
}

extension UIMenuItem {
Expand Down
Loading

0 comments on commit 8f31fb4

Please sign in to comment.