123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- //
- // BFStripSwithView.swift
- // BFRecordScreenKit
- //
- // Created by SanW on 2021/12/15.
- // Copyright © 2021 BytesFlow. All rights reserved.
- //
- import BFCommonKit
- import UIKit
- open class BFStripSwithView: UIView {
- public var itemClickHandle: ((_ sender: UIButton, _ index: Int) -> Void)?
- var currentIndex: Int = 0
- var itemSpace: CGFloat = 4
- var itemHeight: CGFloat = 6
- var itemNormalColor: UIColor = UIColor.white
- var itemSelectedColor: UIColor = UIColor.hexColor(hexadecimal: "#389AFF")
- var strips: Int = 2
- var currentItem: UIButton?
- lazy var selectedBtn: UIButton = {
- let selectedBtn = UIButton(type: .custom)
- return selectedBtn
- }()
- override init(frame: CGRect) {
- super.init(frame: frame)
- }
- public required init?(coder _: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- public init(frame: CGRect, items: Int, index: Int? = nil, normalColor: UIColor? = nil, selectedColor: UIColor? = nil, space: CGFloat = 10) {
- super.init(frame: frame)
- strips = items
- if normalColor != nil {
- itemNormalColor = normalColor!
- }
- if selectedColor != nil {
- itemSelectedColor = selectedColor!
- }
- if index != nil, (index ?? 0) < items {
- currentIndex = index!
- }
- itemSpace = space
- selectedBtn.addCorner(corner: itemHeight / 2)
- selectedBtn.backgroundColor = itemSelectedColor
- }
- override public func layoutSubviews() {
- super.layoutSubviews()
- addSubviews()
- }
- private func addSubviews() {
- if currentItem != nil {
- return
- }
- subviews.forEach { view in
- view.removeFromSuperview()
- }
- if strips > 0 {
- let margin: CGFloat = (frame.width - CGFloat(strips - 1) * itemSpace) / CGFloat(strips)
- for index in 0 ..< strips {
- let itemBtn = UIButton(type: .custom)
- itemBtn.frame = CGRect(x: CGFloat(index) * (itemSpace + margin), y: (frame.height - itemHeight) / 2, width: margin, height: itemHeight)
- itemBtn.tag = index + 1
- // itemBtn.addTarget(self, action: #selector(changeSwitch(sender:)), for: .touchUpInside)
- itemBtn.addCorner(corner: itemHeight / 2)
- itemBtn.backgroundColor = itemNormalColor
- if itemBtn.tag == currentIndex + 1 {
- currentItem = itemBtn
- selectedBtn.frame = itemBtn.frame
- }
- addSubview(itemBtn)
- }
- if selectedBtn.superview == nil {
- addSubview(selectedBtn)
- }
- bringSubviewToFront(selectedBtn)
- }
- }
- @objc public func changeSwitch(sender: UIButton) {
- if sender == currentItem {
- return
- }
- if itemClickHandle != nil {
- itemClickHandle!(sender, Int(sender.tag - 1))
- }
- }
- public func changeSwitchStatus(index: Int) {
- let sender: UIButton? = viewWithTag(Int(index) + 1) as? UIButton
- if sender == nil || sender == currentItem {
- return
- }
- currentItem = sender
- UIView.animate(withDuration: 0.1) { [weak self] in
- self?.selectedBtn.center.x = CGFloat(sender!.center.x)
- }
- }
- }
|