|
|
|
//
|
|
|
|
// DateSegmentView.swift
|
|
|
|
// HPlusFit
|
|
|
|
//
|
|
|
|
// Created by lemo. on 2019/9/17.
|
|
|
|
// Copyright © 2019 lemo. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import RxGesture
|
|
|
|
|
|
|
|
typealias SegmentSelectedClosure = (Int) -> Void
|
|
|
|
class DateSegmentView: UIView {
|
|
|
|
|
|
|
|
let disposeBag = DisposeBag()
|
|
|
|
var selectedClosure: SegmentSelectedClosure?
|
|
|
|
private var lastTimeBtn: UIButton?
|
|
|
|
|
|
|
|
//外部定义的值
|
|
|
|
var selectIndex :Int?{
|
|
|
|
willSet{
|
|
|
|
let width = self.frame.size.width / CGFloat.init((self.titles?.count)!)
|
|
|
|
UIView.animate(withDuration: 0.3) {
|
|
|
|
self.bgView.left = ((newValue)?.int2Float)! * width
|
|
|
|
self.topLabelView.left = -(((newValue)?.int2Float)!) * width
|
|
|
|
self.bgView.layoutIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var titles: Array<String>? {
|
|
|
|
didSet {
|
|
|
|
setTitles()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy var bgView: UIView = {
|
|
|
|
let bg = UIView(frame: CGRect(x: 0, y: 0, width: bounds.width / 3.0, height: bounds.height))
|
|
|
|
bg.gradient(colors: [kHexColor(0x59AAFF), kHexColor(0x2B75FF)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5), locations: [0, 1])
|
|
|
|
return bg
|
|
|
|
}()
|
|
|
|
|
|
|
|
let topLabelView = UIView.init().then {
|
|
|
|
$0.backgroundColor = .clear
|
|
|
|
}
|
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
layer.cornerRadius = 3
|
|
|
|
layer.borderWidth = 0.5
|
|
|
|
layer.borderColor = kHexColor(0xDEDEDE).cgColor
|
|
|
|
layer.masksToBounds = true
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension DateSegmentView {
|
|
|
|
|
|
|
|
/// 设置分段标题
|
|
|
|
func setTitles() {
|
|
|
|
let width = self.frame.size.width / CGFloat.init((titles?.count)!)
|
|
|
|
let height = self.frame.size.height
|
|
|
|
for i in (titles?.indices)! {
|
|
|
|
let titleBtn = UIButton()
|
|
|
|
titleBtn.tag = 1000 + i;
|
|
|
|
titleBtn.setTitle(titles?[i], for: .normal)
|
|
|
|
titleBtn.setTitleColor(kHexColor(0x666666), for: .normal)
|
|
|
|
titleBtn.setTitleColor(kHexColor(0xffffff), for: .selected)
|
|
|
|
titleBtn.titleLabel?.font = SystemMediumFont(14)
|
|
|
|
if i == 0 {
|
|
|
|
titleBtn.isSelected = true
|
|
|
|
titleBtn.titleLabel?.font = SystemMediumFont(16)
|
|
|
|
lastTimeBtn = titleBtn
|
|
|
|
}
|
|
|
|
titleBtn.frame = CGRect.init(x: width*CGFloat.init(i), y: 0, width: width, height: height)
|
|
|
|
titleBtn.rx.tapGesture()
|
|
|
|
.when(UIGestureRecognizer.State.recognized)
|
|
|
|
.subscribe(onNext: { [weak self] sender in
|
|
|
|
guard let `self` = self else { return }
|
|
|
|
self.selectedLabel(sender: sender)
|
|
|
|
})
|
|
|
|
.disposed(by: disposeBag)
|
|
|
|
self.addSubview(titleBtn)
|
|
|
|
}
|
|
|
|
self.insertSubview(bgView, at: 0)
|
|
|
|
topLabelView.frame = self.bounds
|
|
|
|
bgView.addSubview(topLabelView)
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectedLabel(sender: UITapGestureRecognizer) {
|
|
|
|
let width = self.frame.size.width / CGFloat.init((self.titles?.count)!)
|
|
|
|
UIView.animate(withDuration: 0.3, animations: {
|
|
|
|
self.bgView.left = ((sender.view?.tag)! - 1000).int2Float * width
|
|
|
|
self.topLabelView.left = -((sender.view?.tag)! - 1000).int2Float * width
|
|
|
|
self.bgView.layoutIfNeeded()
|
|
|
|
}) { [weak self] _ in
|
|
|
|
// 选中切换颜色
|
|
|
|
if let lastTimeBtn = self?.lastTimeBtn {
|
|
|
|
lastTimeBtn.isSelected = !lastTimeBtn.isSelected
|
|
|
|
lastTimeBtn.titleLabel?.font = SystemMediumFont(14)
|
|
|
|
}
|
|
|
|
if let titleBtn = sender.view as? UIButton {
|
|
|
|
titleBtn.isSelected = !titleBtn.isSelected
|
|
|
|
titleBtn.titleLabel?.font = SystemMediumFont(16)
|
|
|
|
self?.lastTimeBtn = titleBtn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let selected = selectedClosure {
|
|
|
|
selected((sender.view?.tag)! - 1000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Int {
|
|
|
|
var int2Float: CGFloat {
|
|
|
|
return CGFloat.init(self)
|
|
|
|
}
|
|
|
|
}
|