|
@@ -13,13 +13,7 @@ class PQCustomSwitchView: UIView {
|
|
|
var currentItemBtn: UIButton?
|
|
|
/// 按钮点击的回调
|
|
|
var switchChangeHandle: ((_ sender: UIButton) -> Void)?
|
|
|
-
|
|
|
- lazy var slideView: UIView = {
|
|
|
- let slideView = UIView()
|
|
|
- slideView.backgroundColor = UIColor.white
|
|
|
- return slideView
|
|
|
- }()
|
|
|
-
|
|
|
+
|
|
|
override private init(frame: CGRect) {
|
|
|
super.init(frame: frame)
|
|
|
}
|
|
@@ -30,54 +24,48 @@ class PQCustomSwitchView: UIView {
|
|
|
|
|
|
init(frame: CGRect, titles: [String], defaultIndex: Int = 1) {
|
|
|
super.init(frame: frame)
|
|
|
- backgroundColor = PQBFConfig.shared.otherTintColor
|
|
|
- addCorner(corner: frame.height / 2)
|
|
|
- addSubview(slideView)
|
|
|
let itemWidth: CGFloat = frame.width / CGFloat(titles.count)
|
|
|
for (index, itemTitle) in titles.enumerated() {
|
|
|
let itemBtn = UIButton(type: .custom)
|
|
|
- itemBtn.frame = CGRect(x: CGFloat(index) * itemWidth, y: 0, width: itemWidth, height: frame.height)
|
|
|
+ //8 是每一个btn 的间隔
|
|
|
+ itemBtn.frame = CGRect(x: CGFloat(index) * itemWidth + CGFloat(index) * 8, y: 0, width: itemWidth, height: frame.height)
|
|
|
itemBtn.tag = index + 1
|
|
|
itemBtn.setTitle(itemTitle, for: .normal)
|
|
|
- itemBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#999999"), for: .normal)
|
|
|
- itemBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#111111"), for: .selected)
|
|
|
+ itemBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#959595"), for: .normal)
|
|
|
+
|
|
|
+ itemBtn.setTitleColor(UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue), for: .selected)
|
|
|
+ itemBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#3DC1C1"), for: .selected)
|
|
|
itemBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
|
|
|
+
|
|
|
itemBtn.isSelected = index == (defaultIndex - 1)
|
|
|
if index == (defaultIndex - 1) {
|
|
|
- slideView.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: itemBtn.frame.width - 4, height: itemBtn.frame.height - 4))
|
|
|
- slideView.center = itemBtn.center
|
|
|
- slideView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
|
|
|
- slideView.layer.shadowOffset = CGSize.zero
|
|
|
- // 设置偏移量为0,四周都有阴影
|
|
|
- slideView.layer.shadowRadius = 1 // 阴影半径
|
|
|
- slideView.layer.shadowOpacity = 1 // 阴影透明度
|
|
|
- slideView.layer.masksToBounds = false
|
|
|
- slideView.layer.shadowPath = UIBezierPath(roundedRect: slideView.bounds, cornerRadius: slideView.layer.cornerRadius).cgPath
|
|
|
- slideView.addCorner(corner: slideView.frame.height / 2)
|
|
|
- itemBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .medium)
|
|
|
+ itemBtn.isSelected = true
|
|
|
+
|
|
|
currentItemBtn = itemBtn
|
|
|
- } else {
|
|
|
- itemBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
|
|
|
+ itemBtn.backgroundColor = UIColor(red: 0.239, green: 0.757, blue: 0.757, alpha: 0.15)
|
|
|
+ }else{
|
|
|
+ itemBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#F9F9F9")
|
|
|
}
|
|
|
+ itemBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13)
|
|
|
+ itemBtn.addCorner(corner:5)
|
|
|
addSubview(itemBtn)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// 按钮点击事件
|
|
|
/// - Parameter sender: <#sender description#>
|
|
|
/// - Returns: <#description#>
|
|
|
@objc func btnClick(sender: UIButton) {
|
|
|
currentItemBtn?.isSelected = false
|
|
|
currentItemBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 13)
|
|
|
+ currentItemBtn?.backgroundColor = UIColor.hexColor(hexadecimal: "#F9F9F9")
|
|
|
sender.isSelected = true
|
|
|
- sender.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .medium)
|
|
|
+ sender.backgroundColor = UIColor(red: 0.239, green: 0.757, blue: 0.757, alpha: 0.15)
|
|
|
+// sender.backgroundColor = .red
|
|
|
currentItemBtn = sender
|
|
|
if switchChangeHandle != nil {
|
|
|
switchChangeHandle!(sender)
|
|
|
}
|
|
|
- UIView.animate(withDuration: 0.3, delay: 0, options: .allowUserInteraction) { [weak self] in
|
|
|
- self?.slideView.center.x = sender.center.x
|
|
|
- } completion: { _ in
|
|
|
- }
|
|
|
+
|
|
|
}
|
|
|
}
|