TitleView.swift 3.86 KB
//
//  SearchTitleView.swift
//  zc
//
//  Created by wyp on 2020/11/9.
//  Copyright © 2020 wyp. All rights reserved.
//

import UIKit

protocol TitleViewDelegate: NSObjectProtocol {
    
    func didClickButton(sender: UIButton)
    
    
}


class TitleView: UIView {
    weak var delegate: TitleViewDelegate?
    
    private lazy var userButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(clickButton(sender:)), for: .touchUpInside)
//        button.setTitle("用户", for: .normal)
        button.tag = 1
        button.setTitleColor(UIColor.rgbColorFromHex(0x999999), for: .normal)
        button.titleLabel?.font = RegularFont(14)
        return button
    }()
    
    private lazy var newsButton: UIButton = {
        let button = UIButton()
        button.tag = 0
        button.addTarget(self, action: #selector(clickButton(sender:)), for: .touchUpInside)
//        button.setTitle("内容", for: .normal)
        button.setTitleColor(UIColor.rgbColorFromHex(0xEE6C4D), for: .normal)
        button.titleLabel?.font = RegularFont(16)
        return button
    }()
    
    
    init(frame: CGRect, titles: [String] = ["内容", "用户"]) {
        super.init(frame: frame)
//        backgroundColor = .red
        setupSubViews(titles: titles)
    }
    

    @objc func clickButton(sender: UIButton) {
        delegate?.didClickButton(sender: sender)
        
        if sender.tag == 0 {
            newsButton.titleLabel?.font = RegularFont(16)
            newsButton.setTitleColor(UIColor.rgbColorFromHex(0xEE6C4D), for: .normal)
            userButton.setTitleColor(UIColor.rgbColorFromHex(0x999999), for: .normal)
            userButton.titleLabel?.font = RegularFont(14)
            
        } else if sender.tag == 1 {
            userButton.titleLabel?.font = RegularFont(16)
            userButton.setTitleColor(UIColor.rgbColorFromHex(0xEE6C4D), for: .normal)
            newsButton.setTitleColor(UIColor.rgbColorFromHex(0x999999), for: .normal)
            newsButton.titleLabel?.font = RegularFont(14)
        }
    }
    
    func refreshButton(index: Int) {
        if index == 0 {
            newsButton.titleLabel?.font = RegularFont(16)
            newsButton.setTitleColor(UIColor.rgbColorFromHex(0xEE6C4D), for: .normal)
            userButton.setTitleColor(UIColor.rgbColorFromHex(0x999999), for: .normal)
            userButton.titleLabel?.font = RegularFont(14)
            
        } else if index == 1 {
            userButton.titleLabel?.font = RegularFont(16)
            userButton.setTitleColor(UIColor.rgbColorFromHex(0xEE6C4D), for: .normal)
            newsButton.setTitleColor(UIColor.rgbColorFromHex(0x999999), for: .normal)
            newsButton.titleLabel?.font = RegularFont(14)
        }
    }
    
    
    func setupSubViews(titles: [String]) {
//        addSubview(sliderScrollView)
//        addSubview(pageBottomLineView)
        userButton.setTitle(titles[1], for: .normal)
        newsButton.setTitle(titles[0], for: .normal)
        addSubview(newsButton)
        addSubview(userButton)
        newsButton.snp.makeConstraints { (make) in
            make.width.equalTo(50)
            make.height.equalTo(30)
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview().multipliedBy(0.67)
        }
        userButton.snp.makeConstraints { (make) in
            make.width.equalTo(50)
            make.height.equalTo(30)
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview().multipliedBy(1.33)
        }
        
        
        
    }

    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}