几句话搞定一个选择标签控件
效果图:
我们在项目开发过程中,遇到过上图的效果,每次写都很麻烦,所以这次就自己封装了一下,以前一直用别人的,现在也想自己封装,首次尝试,望大家提宝贵意见
用的时候比较简单,只需要设置代理之后,传入标题和左右两个数组即可。因为项目是swift写的,所以只写了swift版。
因为我们的项目文字和图标是随主题色改变的,这里用到一个比较简便的方法,
设置image的RenderingMode(此属性iOS7之后有了),然后设置imageView的tintcolor即可,如果没有此需求,可以去掉
leftImgView.image = UIImage(named:"ic_shouqi")?.withRenderingMode(.alwaysTemplate)
leftImgView.tintColor = UIColor(hexString: GlobalVariable.themeColor)
其他只需要导入两个类即可:
//
// ActivityTopShowView.swift
// QingkCloud_Swift
//
// Created by bxfMini on 2019/9/12.
//
import UIKit
protocol ActivityTopViewCellClickDelegate : NSObjectProtocol {
func deliverData(str:String,index:Int,mark:Int)
}
class ActivityTopShowView: UIView {
var isShow:Bool!
var isRightShow:Bool!
var leftImgView:UIImageView!
var rightImgView:UIImageView!
var leftBtn:UIButton!
var rightBtn:UIButton!
var leftLab:UILabel!
var rightLab:UILabel!
var leftArr:NSArray!
var rightArr:NSArray!
weak var delegate:ActivityTopViewCellClickDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
isShow = false
isRightShow = false
creatUI()
}
func creatUI(){
let lefttopView = UIView.init()
lefttopView.backgroundColor = UIColor.white
self.addSubview(lefttopView)
leftLab = UILabel.init()
leftLab.text = leftTitle
leftLab.font = UIFont.systemFont(ofSize: 15)
leftLab.textColor = UIColor(hexString: "#666666")
lefttopView.addSubview(leftLab)
leftImgView = UIImageView.init()
leftImgView.image = UIImage(named: "ic_zhankai")
lefttopView.addSubview(leftImgView)
leftBtn = UIButton(type: .custom)
leftBtn.tag = 1
leftBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
lefttopView.addSubview(leftBtn)
lefttopView.snp.makeConstraints { (make) in
make.top.left.equalTo(0)
make.width.equalTo(kScreenWidth/2)
make.height.equalTo(44)
}
leftLab.snp.makeConstraints { (make) in
make.centerY.centerX.equalTo(lefttopView)
make.width.equalTo(62)
make.height.equalTo(14)
}
leftImgView.snp.makeConstraints { (make) in
make.centerY.equalTo(lefttopView)
make.width.equalTo(10)
make.height.equalTo(6)
make.left.equalTo(leftLab.snp.right).offset(5)
}
leftBtn.snp.makeConstraints { (make) in
make.left.top.bottom.equalTo(0)
make.width.equalTo(kScreenWidth/2)
}
let righttopView = UIView.init()
righttopView.backgroundColor = UIColor.white
self.addSubview(righttopView)
rightLab = UILabel.init()
rightLab.text = rightTitle
rightLab.font = UIFont.systemFont(ofSize: 15)
rightLab.textColor = UIColor(hexString: "#666666")
righttopView.addSubview(rightLab)
rightImgView = UIImageView.init()
rightImgView.image = UIImage(named: "ic_zhankai")
righttopView.addSubview(rightImgView)
rightBtn = UIButton(type: .custom)
rightBtn.tag = 2
rightBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
righttopView.addSubview(rightBtn)
righttopView.snp.makeConstraints { (make) in
make.top.right.equalTo(0)
make.width.equalTo(kScreenWidth/2)
make.height.equalTo(44)
}
rightLab.snp.makeConstraints { (make) in
make.centerY.centerX.equalTo(righttopView)
make.width.equalTo(62)
make.height.equalTo(14)
}
rightImgView.snp.makeConstraints { (make) in
make.centerY.equalTo(righttopView)
make.width.equalTo(10)
make.height.equalTo(6)
make.left.equalTo(rightLab.snp.right).offset(5)
}
rightBtn.snp.makeConstraints { (make) in
make.right.top.bottom.equalTo(0)
make.width.equalTo(kScreenWidth/2)
}
}
var leftTitle:String = "" {
didSet{
leftLab.text = leftTitle
}
}
var rightTitle:String = "" {
didSet{
rightLab.text = rightTitle
}
}
@objc func btnClick(sender:UIButton){
if sender.tag == 1 {
isRightShow = false
isShow = !isShow
if isShow{
leftImgView.image = UIImage(named:"ic_shouqi")?.withRenderingMode(.alwaysTemplate)
leftImgView.tintColor = UIColor(hexString: GlobalVariable.themeColor)
leftLab.textColor = UIColor(hexString: GlobalVariable.themeColor)
rightImgView.image = UIImage(named: "ic_zhankai")
rightLab.textColor = UIColor(hexString: "#666666")
topView.bingDingData(arr: leftArr, mark: 1)
self.superview!.addSubview(topView)
}else{
topView.removeFromSuperview()
leftImgView.image = UIImage(named: "ic_zhankai")
leftLab.textColor = UIColor(hexString: "#666666")
}
}else{
isShow = false
isRightShow = !isRightShow
if isRightShow{
rightImgView.image = UIImage(named:"ic_shouqi")?.withRenderingMode(.alwaysTemplate)
rightImgView.tintColor = UIColor(hexString: GlobalVariable.themeColor)
rightLab.textColor = UIColor(hexString: GlobalVariable.themeColor)
leftImgView.image = UIImage(named: "ic_zhankai")
leftLab.textColor = UIColor(hexString: "#666666")
topView.bingDingData(arr: rightArr, mark: 2)
self.superview!.addSubview(topView)
}else{
topView.removeFromSuperview()
rightImgView.image = UIImage(named: "ic_zhankai")
rightLab.textColor = UIColor(hexString: "#666666")
}
}
}
private lazy var topView:ActivityTopView = {
var topView = ActivityTopView.init(frame: CGRect(x: 0, y: 44, width: kScreenWidth, height: kScreenHeight))
topView.touchBlock = { () in
self.isShow = false
self.isRightShow = false
self.rightImgView.image = UIImage(named: "ic_zhankai")
self.rightLab.textColor = UIColor(hexString: "#666666")
self.leftImgView.image = UIImage(named: "ic_zhankai")
self.leftLab.textColor = UIColor(hexString: "#666666")
}
topView.block = {(str,index,mark) in
if self.delegate != nil{
self.delegate?.deliverData(str: str, index: index, mark: mark)
}
}
return topView
}()
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//
// ActivityTopView.swift
// QingkCloud_Swift
//
// Created by bxfMini on 2019/9/9.
//
import UIKit
class ActivityTopView: UIView,UITableViewDelegate,UITableViewDataSource {
var titleArr:NSArray!
var block:((String,Int,Int) -> Void)?
var selectIndexParh:IndexPath!
var rightSelectIndexParh:IndexPath!
var mark:Int?
var touchBlock:(() -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
selectIndexParh = IndexPath.init(row: 0, section: 0)
rightSelectIndexParh = IndexPath.init(row: 0, section: 0)
self.addSubview(tableV)
self.backgroundColor = UIColor(r: 0, g: 0, b: 0, a: 0.6)
}
private lazy var tableV:UITableView = {
var tableView:UITableView!
tableView = UITableView.init()
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: NSStringFromClass(UITableViewCell.classForCoder()))
return tableView
}()
override func layoutSubviews() {
tableV.snp.makeConstraints { (make) in
make.top.left.right.equalTo(0)
make.height.equalTo(titleArr.count*44)
}
}
func bingDingData(arr:NSArray,mark:Int){
titleArr = arr
self.mark = mark
tableV.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titleArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(UITableViewCell.classForCoder()), for: indexPath)
cell.selectionStyle = UITableViewCell.SelectionStyle.none
if mark == 1 {
if indexPath.row == selectIndexParh.row {
cell.textLabel?.textColor = UIColor(hexString: GlobalVariable.themeColor)
}else{
cell.textLabel?.textColor = UIColor.black
}
}else{
if indexPath.row == rightSelectIndexParh.row {
cell.textLabel?.textColor = UIColor(hexString: GlobalVariable.themeColor)
}else{
cell.textLabel?.textColor = UIColor.black
}
}
cell.textLabel?.text = titleArr[indexPath.row] as? String
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if mark == 1 {
if selectIndexParh == indexPath{
return
}
let newCell :UITableViewCell = tableView.cellForRow(at: indexPath)!
newCell.textLabel?.textColor = UIColor(hexString: GlobalVariable.themeColor)
let oldCell:UITableViewCell = tableView.cellForRow(at: selectIndexParh)!
oldCell.textLabel?.textColor = UIColor.black
selectIndexParh = indexPath
}else{
if rightSelectIndexParh == indexPath{
return
}
let newCell :UITableViewCell = tableView.cellForRow(at: indexPath)!
newCell.textLabel?.textColor = UIColor(hexString: GlobalVariable.themeColor)
let oldCell:UITableViewCell = tableView.cellForRow(at: rightSelectIndexParh)!
oldCell.textLabel?.textColor = UIColor.black
rightSelectIndexParh = indexPath
}
if block != nil {
block!(titleArr[indexPath.row] as! String,indexPath.row, mark!)
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.removeFromSuperview()
if touchBlock != nil {
touchBlock!()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}