FSPagerView/Sources/FSPagerViewCell.swift

140 lines
4.5 KiB
Swift

//
// 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)
}
}
}