forked from WenchaoD/FSPagerView
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFSPagerViewCell.swift
152 lines (133 loc) · 4.81 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
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// 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.
@objc
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.
@objc
open var imageView: UIImageView? {
if let _ = _imageView {
return _imageView
}
let imageView = UIImageView(frame: .zero)
self.contentView.addSubview(imageView)
_imageView = imageView
return imageView
}
fileprivate weak var _textLabel: UILabel?
fileprivate 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? {
guard _selectedForegroundView == nil else {
return _selectedForegroundView
}
guard let imageView = _imageView else {
return nil
}
let view = UIView(frame: imageView.bounds)
imageView.addSubview(view)
_selectedForegroundView = view
return view
}
open override var isHighlighted: Bool {
set {
super.isHighlighted = newValue
if newValue {
self.selectedForegroundView?.layer.backgroundColor = self.selectionColor.cgColor
} else if !super.isSelected {
self.selectedForegroundView?.layer.backgroundColor = UIColor.clear.cgColor
}
}
get {
return super.isHighlighted
}
}
open override var isSelected: Bool {
set {
super.isSelected = newValue
self.selectedForegroundView?.layer.backgroundColor = newValue ? self.selectionColor.cgColor : UIColor.clear.cgColor
}
get {
return super.isSelected
}
}
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.clear
self.backgroundColor = UIColor.clear
self.contentView.layer.shadowColor = UIColor.black.cgColor
self.contentView.layer.shadowRadius = 5
self.contentView.layer.shadowOpacity = 0.75
self.contentView.layer.shadowOffset = .zero
}
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)
}
}
}