2 // FolioReaderPageIndicator.swift
5 // Created by Heberti Almeida on 10/09/15.
6 // Copyright (c) 2015 Folio Reader. All rights reserved.
11 class FolioReaderPageIndicator: UIView {
12 var pagesLabel: UILabel!
13 var minutesLabel: UILabel!
14 var totalMinutes: Int!
16 var currentPage: Int = 1 {
17 didSet { self.reloadViewWithPage(self.currentPage) }
20 fileprivate var readerConfig: FolioReaderConfig
21 fileprivate var folioReader: FolioReader
23 init(frame: CGRect, readerConfig: FolioReaderConfig, folioReader: FolioReader) {
24 self.readerConfig = readerConfig
25 self.folioReader = folioReader
27 super.init(frame: frame)
29 let color = self.folioReader.isNight(self.readerConfig.nightModeBackground, UIColor.white)
30 backgroundColor = color
31 layer.shadowColor = color.cgColor
32 layer.shadowOffset = CGSize(width: 0, height: -6)
33 layer.shadowOpacity = 1
34 layer.shadowRadius = 4
35 layer.shadowPath = UIBezierPath(rect: bounds).cgPath
36 layer.rasterizationScale = UIScreen.main.scale
37 layer.shouldRasterize = true
39 pagesLabel = UILabel(frame: CGRect.zero)
40 pagesLabel.font = UIFont(name: "Avenir-Light", size: 10)!
41 pagesLabel.textAlignment = NSTextAlignment.right
42 addSubview(pagesLabel)
44 minutesLabel = UILabel(frame: CGRect.zero)
45 minutesLabel.font = UIFont(name: "Avenir-Light", size: 10)!
46 minutesLabel.textAlignment = NSTextAlignment.right
47 // minutesLabel.alpha = 0
48 addSubview(minutesLabel)
51 required init?(coder aDecoder: NSCoder) {
52 fatalError("storyboards are incompatible with truth and beauty")
55 func reloadView(updateShadow: Bool) {
56 minutesLabel.sizeToFit()
57 pagesLabel.sizeToFit()
59 let fullW = pagesLabel.frame.width + minutesLabel.frame.width
60 minutesLabel.frame.origin = CGPoint(x: frame.width/2-fullW/2, y: 2)
61 pagesLabel.frame.origin = CGPoint(x: minutesLabel.frame.origin.x+minutesLabel.frame.width, y: 2)
64 layer.shadowPath = UIBezierPath(rect: bounds).cgPath
70 let color = self.folioReader.isNight(self.readerConfig.nightModeBackground, UIColor.white)
71 backgroundColor = color
73 // Animate the shadow color change
74 let animation = CABasicAnimation(keyPath: "shadowColor")
75 let currentColor = UIColor(cgColor: layer.shadowColor!)
76 animation.fromValue = currentColor.cgColor
77 animation.toValue = color.cgColor
78 animation.fillMode = kCAFillModeForwards
79 animation.isRemovedOnCompletion = false
80 animation.duration = 0.6
81 animation.delegate = self
82 animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
83 layer.add(animation, forKey: "shadowColor")
85 minutesLabel.textColor = self.folioReader.isNight(UIColor(white: 1, alpha: 0.3), UIColor(white: 0, alpha: 0.6))
86 pagesLabel.textColor = self.folioReader.isNight(UIColor(white: 1, alpha: 0.6), UIColor(white: 0, alpha: 0.9))
89 fileprivate func reloadViewWithPage(_ page: Int) {
90 let pagesRemaining = self.folioReader.needsRTLChange ? totalPages-(totalPages-page+1) : totalPages-page
92 if pagesRemaining == 1 {
93 pagesLabel.text = " " + self.readerConfig.localizedReaderOnePageLeft
96 // pagesLabel.text = " \(pagesRemaining) " + self.readerConfig.localizedReaderManyPagesLeft
98 else if pagesRemaining == 0 {
99 pagesLabel.text = "0 stron do końca"
101 else if pagesRemaining < 5 {
102 pagesLabel.text = " \(pagesRemaining) " + self.readerConfig.localizedReaderManyPagesLeft
105 pagesLabel.text = " \(pagesRemaining) " + "stron do końca"
110 minutesLabel.text = ""
111 // let minutesRemaining = Int(ceil(CGFloat((pagesRemaining * totalMinutes)/totalPages)))
112 // if minutesRemaining > 1 {
113 // minutesLabel.text = "\(minutesRemaining) " + self.readerConfig.localizedReaderManyMinutes+" ·"
114 // } else if minutesRemaining == 1 {
115 // minutesLabel.text = self.readerConfig.localizedReaderOneMinute+" ·"
117 // minutesLabel.text = self.readerConfig.localizedReaderLessThanOneMinute+" ·"
120 reloadView(updateShadow: false)
124 extension FolioReaderPageIndicator: CAAnimationDelegate {
125 func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
126 // Set the shadow color to the final value of the animation is done
127 if let keyPath = anim.value(forKeyPath: "keyPath") as? String , keyPath == "shadowColor" {
128 let color = self.folioReader.isNight(self.readerConfig.nightModeBackground, UIColor.white)
129 layer.shadowColor = color.cgColor