123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- //
- // MCNavBarSegmentValue1View.swift
- // HCQuanfangtong
- //
- // Created by Apple on 2022/9/22.
- // Copyright © 2022 Jyp. All rights reserved.
- //
- import UIKit
- import RxSwift
- import RxCocoa
- /// <#Description#>
- class MCNavBarSegmentValue1View: MCNavBarView {
- init(dataSource: [String], style: MCNavBarStyle? = nil, tap: ((Int)->Void)?, defaultSelectedIndex: Int = 0){
- super.init(title: nil, subTitle: nil, style: style)
- self.contentView.addArrangedSubview(self.stackView)
- self.stackView.mas_makeConstraints { make in
- make?.height.offset()(KNavBarHeight)
- }
-
- for i in 0..<dataSource.count{
- let segmentView = NavSegmentView.init(name: dataSource[i], style: self.navStyle)
- segmentView.tap = {[unowned self] in
- segmentView.scaleText()
- _ = self.stackView.arrangedSubviews.filter { e in
- e != segmentView
- }.map { e in
- (e as! NavSegmentView).identifyText()
- }
- tap?(i)
- }
- self.stackView.addArrangedSubview(segmentView)
- segmentView.mas_makeConstraints { make in
- make?.height.offset()(KNavBarHeight)
- }
- }
- if self.stackView.arrangedSubviews.canGet(at: defaultSelectedIndex).0{
- (self.stackView.arrangedSubviews[defaultSelectedIndex] as! NavSegmentView).scaleText()
- _ = self.stackView.arrangedSubviews.filter { e in
- e != self.stackView.arrangedSubviews[defaultSelectedIndex]
- }.map { e in
- (e as! NavSegmentView).identifyText()
- }
- }
- self.resetConstraint()
- }
- //MARK: - Private Property
- private lazy var stackView: UIStackView! = {
- let stackView = UIStackView.init(frame: CGRect.zero)
- stackView.alignment = .center
- stackView.axis = .horizontal
- stackView.distribution = .fillEqually
- return stackView
- }()
-
-
- //MARK: - Override MCNavBarDelegate
- override var type: MCNavBarType{
- .segment
- }
-
- override func setNavTitle(_ title: String?) {
- if let index = Convert.toInt(title) {
- if self.stackView.arrangedSubviews.canGet(at: index).0{
- (self.stackView.arrangedSubviews[index] as! NavSegmentView).scaleText()
- }
- }
- }
- override func setNavSubTitle(_ title: String?) {
- return
- }
-
- //MARK: -
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
-
- }
- fileprivate class NavSegmentView: UIView{
-
- /// 点击事件
- var tap: (()->Void)?{
- didSet{
- self.addTapAction {[unowned self] _ in
- if self.isSelected{
- return
- }
- self.isSelected = true
- self.tap?()
- }
- }
- }
-
- init(name: String, style: MCNavBarStyle){
- super.init(frame: CGRect.zero)
- self.style = style
- self.textLabel = UILabel.init(frame: CGRect.zero)
- self.textLabel.text = name
- self.textLabel.textAlignment = .center
- self.textLabel.textColor = style.styleForSubTitleColor
- self.textLabel.font = style.styleForTitleFont
- self.addSubview(self.textLabel)
- self.textLabel.mas_makeConstraints { make in
- make?.center.mas_equalTo()(self)
- }
- }
-
-
-
- /// 放大text
- func scaleText(){
- if !self.isSelected{
- self.isSelected = true
- }
- self.textLabel.transform = CGAffineTransform.identity
- self.textLabel.textColor = style.styleForTitleColor
- self.textLabel.font = style.styleForTitleFont
- self.textLabel.transform = CGAffineTransform.init(scaleX: self.style.styleForScaling, y: self.style.styleForScaling)
- }
-
- /// 还原text
- func identifyText(){
- if self.isSelected{
- self.isSelected = false
- }
- self.textLabel.transform = CGAffineTransform.identity
- self.textLabel.textColor = style.styleForScalingBeforeTitleColor
- }
-
- //MARK: - Private Property
-
- /// 内容
- private var textLabel: UILabel!
- private var style: MCNavBarStyle!
- private var isSelected: Bool = false
-
- //MARK: -
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
-
-
- }
|