This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 772
/
FolioReaderPageIndicator.swift
91 lines (74 loc) · 3.34 KB
/
FolioReaderPageIndicator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// FolioReaderPageIndicator.swift
// FolioReaderKit
//
// Created by Heberti Almeida on 10/09/15.
// Copyright (c) 2015 Folio Reader. All rights reserved.
//
import UIKit
class FolioReaderPageIndicator: UIView {
var pagesLabel: UILabel!
var minutesLabel: UILabel!
var totalMinutes: Int!
var totalPages: Int!
var currentPage: Int = 1 {
didSet { self.reloadViewWithPage(self.currentPage) }
}
override init(frame: CGRect) {
super.init(frame: frame)
let color = isNight(readerConfig.nightModeBackground, UIColor.whiteColor())
backgroundColor = color
layer.shadowColor = color.CGColor
layer.shadowOffset = CGSize(width: 0, height: -6)
layer.shadowOpacity = 1
layer.shadowRadius = 4
layer.shadowPath = UIBezierPath(rect: bounds).CGPath
layer.rasterizationScale = UIScreen.mainScreen().scale
layer.shouldRasterize = true
pagesLabel = UILabel(frame: CGRectZero)
pagesLabel.font = UIFont(name: "Avenir-Light", size: 10)!
pagesLabel.textAlignment = NSTextAlignment.Right
addSubview(pagesLabel)
minutesLabel = UILabel(frame: CGRectZero)
minutesLabel.font = UIFont(name: "Avenir-Light", size: 10)!
minutesLabel.textAlignment = NSTextAlignment.Right
// minutesLabel.alpha = 0
addSubview(minutesLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("storyboards are incompatible with truth and beauty")
}
func reloadView(updateShadow updateShadow: Bool) {
minutesLabel.sizeToFit()
pagesLabel.sizeToFit()
let fullW = pagesLabel.frame.width + minutesLabel.frame.width
minutesLabel.frame.origin = CGPoint(x: frame.width/2-fullW/2, y: 2)
pagesLabel.frame.origin = CGPoint(x: minutesLabel.frame.origin.x+minutesLabel.frame.width, y: 2)
if updateShadow {
layer.shadowPath = UIBezierPath(rect: bounds).CGPath
// Update colors
let color = isNight(readerConfig.nightModeBackground, UIColor.whiteColor())
backgroundColor = color
layer.shadowColor = color.CGColor
minutesLabel.textColor = isNight(UIColor(white: 5, alpha: 0.3), UIColor(white: 0, alpha: 0.6))
pagesLabel.textColor = isNight(UIColor(white: 5, alpha: 0.6), UIColor(white: 0, alpha: 0.9))
}
}
private func reloadViewWithPage(page: Int) {
let pagesRemaining = FolioReader.needsRTLChange ? totalPages-(totalPages-page+1) : totalPages-page
if pagesRemaining == 1 {
pagesLabel.text = " "+readerConfig.localizedReaderOnePageLeft
} else {
pagesLabel.text = " \(pagesRemaining) "+readerConfig.localizedReaderManyPagesLeft
}
let minutesRemaining = Int(ceil(CGFloat((pagesRemaining * totalMinutes)/totalPages)))
if minutesRemaining > 1 {
minutesLabel.text = "\(minutesRemaining) "+readerConfig.localizedReaderManyMinutes+" ·"
} else if minutesRemaining == 1 {
minutesLabel.text = readerConfig.localizedReaderOneMinute+" ·"
} else {
minutesLabel.text = readerConfig.localizedReaderLessThanOneMinute+" ·"
}
reloadView(updateShadow: false)
}
}