Tusker/Tusker/Views/Status/StatusCollapseButton.swift

98 lines
2.8 KiB
Swift

//
// StatusCollapseButton.swift
// Tusker
//
// Created by Shadowfacts on 11/3/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import UIKit
class StatusCollapseButton: UIView {
var action: (()->Void)!
var title: String {
get {
return lblTitle.text ?? ""
}
set {
lblTitle.text = newValue
}
}
private var lblTitle = UILabel()
private var imgView = UIImageView()
private var isCollapsed = true
convenience init(action: @escaping (()->Void)) {
self.init(frame: CGRect.zero)
self.action = action
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// Eat event
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
// Eat event
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
// Eat event
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if isCollapsed {
imgView.image = UIImage(systemName: "chevron.compact.up")
accessibilityLabel = NSLocalizedString("Collapse Status", comment: "collapse status button accessibility label")
} else {
imgView.image = UIImage(systemName: "chevron.compact.down")
accessibilityLabel = NSLocalizedString("Expand Status", comment: "expand status button accessibility label")
}
isCollapsed.toggle()
action()
}
// MARK: - Private Methods
private func setup() {
self.isUserInteractionEnabled = true
layer.cornerRadius = 8
backgroundColor = .lightGray
accessibilityLabel = NSLocalizedString("Expand Status", comment: "expand status button accessibility label")
// Title
lblTitle.translatesAutoresizingMaskIntoConstraints = false
lblTitle.textColor = .white
lblTitle.textAlignment = .center
addSubview(lblTitle)
// Chevron
imgView.translatesAutoresizingMaskIntoConstraints = false
imgView.tintColor = .white
imgView.image = UIImage(systemName: "chevron.compact.down")
addSubview(imgView)
NSLayoutConstraint.activate([
// Main view
heightAnchor.constraint(equalToConstant: 30),
// Title
lblTitle.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
lblTitle.topAnchor.constraint(equalTo: topAnchor, constant: 8),
lblTitle.trailingAnchor.constraint(equalTo: imgView.leadingAnchor, constant: -4),
lblTitle.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8),
// Chevron
imgView.widthAnchor.constraint(equalToConstant: 20),
imgView.topAnchor.constraint(equalTo: topAnchor, constant: 5),
imgView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8),
imgView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5)
])
}
}