forked from WenchaoD/FSPagerView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFSPagerViewCell.swift
139 lines (120 loc) · 4.53 KB
/
FSPagerViewCell.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// FSPagerViewCell.swift
// FSPagerView
//
// Created by Wenchao Ding on 17/12/2016.
// Copyright © 2016 Wenchao Ding. All rights reserved.
//
import UIKit
open class FSPagerViewCell: UICollectionViewCell {
/// Returns the label used for the main textual content of the pager view cell.
open var textLabel: UILabel? {
if let _ = _textLabel {
return _textLabel
}
let view = UIView(frame: .zero)
view.isUserInteractionEnabled = false
view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
let textLabel = UILabel(frame: .zero)
textLabel.textColor = .white
textLabel.font = UIFont.preferredFont(forTextStyle: .body)
self.contentView.addSubview(view)
view.addSubview(textLabel)
textLabel.addObserver(self, forKeyPath: "font", options: [.old,.new], context: kvoContext)
_textLabel = textLabel
return textLabel
}
/// Returns the image view of the pager view cell. Default is nil.
open var imageView: UIImageView? {
if let _ = _imageView {
return _imageView
}
let imageView = UIImageView(frame: .zero)
self.contentView.addSubview(imageView)
_imageView = imageView
return imageView
}
internal weak var _textLabel: UILabel?
internal weak var _imageView: UIImageView?
fileprivate let kvoContext = UnsafeMutableRawPointer(bitPattern: 0)
fileprivate let selectionColor = UIColor(white: 0.2, alpha: 0.2)
fileprivate weak var _selectedForegroundView: UIView?
fileprivate var selectedForegroundView: UIView? {
if let _ = _selectedForegroundView {
return _selectedForegroundView
}
let view = UIView(frame: self.contentView.bounds)
self.contentView.addSubview(view)
_selectedForegroundView = view
return view
}
open override var isHighlighted: Bool {
didSet {
if self.isHighlighted {
self.selectedForegroundView?.layer.backgroundColor = self.selectionColor.cgColor
} else if !self.isSelected {
self.selectedForegroundView?.layer.backgroundColor = UIColor.clear.cgColor
}
}
}
open override var isSelected: Bool {
didSet {
self.selectedForegroundView?.layer.backgroundColor = self.isSelected ? self.selectionColor.cgColor : UIColor.clear.cgColor
}
}
public override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
fileprivate func commonInit() {
self.contentView.backgroundColor = UIColor.white
self.contentView.layer.shadowColor = UIColor.black.cgColor
self.contentView.layer.shadowRadius = 5
self.contentView.layer.shadowOpacity = 0.75
self.contentView.layer.shadowOffset = .zero
self.backgroundColor = UIColor.white
}
deinit {
if let textLabel = _textLabel {
textLabel.removeObserver(self, forKeyPath: "font", context: kvoContext)
}
}
override open func layoutSubviews() {
super.layoutSubviews()
if let imageView = _imageView {
imageView.frame = self.contentView.bounds
}
if let textLabel = _textLabel {
textLabel.superview!.frame = {
var rect = self.contentView.bounds
let height = textLabel.font.pointSize*1.5
rect.size.height = height
rect.origin.y = self.contentView.frame.height-height
return rect
}()
textLabel.frame = {
var rect = textLabel.superview!.bounds
rect = rect.insetBy(dx: 8, dy: 0)
rect.size.height -= 1
rect.origin.y += 1
return rect
}()
}
if let selectedForegroundView = _selectedForegroundView {
selectedForegroundView.frame = self.contentView.bounds
}
}
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if context == kvoContext {
if keyPath == "font" {
self.setNeedsLayout()
}
} else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
}