實(shí)現(xiàn)效果:
- frame可自定義,進(jìn)行了適配.注意點(diǎn)在于為了保證顯示效果,view.height不能小于view.width/3.
- 可在init方法中設(shè)定默認(rèn)顯示數(shù)字,以及最大可選數(shù)字.
- 通過(guò)block進(jìn)行點(diǎn)擊加減按鈕的方法回調(diào)
- 可直接調(diào)用只讀屬性來(lái)獲取當(dāng)前顯示數(shù)字.
實(shí)現(xiàn)方式:
添加subView,左側(cè)button,中間顯示Label,右側(cè)button.并且為左右button設(shè)定可點(diǎn)狀態(tài)和不可點(diǎn)狀態(tài).
添加顯示數(shù)字屬性以及最大可選數(shù)屬性.
重寫init方法實(shí)現(xiàn)適配,設(shè)定view.height不能小于view.width/3
添加block回調(diào).
1.添加subView,左側(cè)button,中間顯示Label,右側(cè)button.并且為左右button設(shè)定可點(diǎn)狀態(tài)和不可點(diǎn)狀態(tài).
/// 左側(cè)減按鈕
private let subtractButton: UIButton = {
let button = UIButton()
button.isEnabled = false
button.tag = 0
button.setBackgroundImage(UIImage(named: "selectNumber_subtractIsEnableTrue"), for: .normal)
button.setBackgroundImage(UIImage(named: "selectNumber_subtractIsEnableFalse"), for: .disabled)
return button
}()
/// 中間顯示label
private lazy var showLabel: UILabel = {
let label = UILabel()
label.backgroundColor = UIColor(red: 246/255, green: 246/255, blue: 246/255, alpha: 1)
label.textColor = UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1)
label.text = "\(_selectedNumber)"
label.font = UIFont.systemFont(ofSize: 13)
label.textAlignment = .center
return label
}()
/// 右側(cè)加按鈕
private let addButton: UIButton = {
let button = UIButton()
button.tag = 1
button.setBackgroundImage(UIImage(named: "selectNumber_addEnableTrue"), for: .normal)
button.setBackgroundImage(UIImage(named: "selectNumber_addEnableFalse"), for: .disabled)
return button
}()
2.添加顯示數(shù)字屬性以及最大可選數(shù)屬性.
/// 最大顯示數(shù),可以通過(guò)初始化設(shè)置
private var maxSelectNumber: Int = 99
/// 選擇數(shù)量,private,外部不可調(diào)用
private var _selectedNumber: Int = 1{
/// 當(dāng)選擇數(shù)量修改時(shí)進(jìn)行顯示label的文字修改,以及加減按鈕的狀態(tài)修改.
didSet{
self.subtractButton.isEnabled = _selectedNumber == 1 ? false : true
self.addButton.isEnabled = _selectedNumber == maxSelectNumber ? false : true
self.showLabel.text = "\(_selectedNumber)"
}
}
/// 外部調(diào)用當(dāng)前顯示數(shù)量,只讀,不可修改.防止異常
public var selectedNumber: Int{
get {
return _selectedNumber
}
}
3.重寫init方法實(shí)現(xiàn)適配,設(shè)定view.height不能小于view.width/3
init(frame: CGRect, defaultNumber: Int = 1, maxNumber: Int = 99){
/// 加入frame限制,保證按鈕size小于view.width*1/3,保證顯示效果
let frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.width, height: frame.height < frame.width/3 ? frame.height : frame.width/3)
super.init(frame: frame)
self.maxSelectNumber = maxNumber
self.setValue(defaultNumber, forUndefinedKey: "selectedNumber")
drawMyView()
}
4.添加block回調(diào).
/// 修改數(shù)量回調(diào),減回調(diào)false,加回調(diào)true
public var backEditNumber: ((Bool) -> ())?
/// 點(diǎn)擊加減按鈕
@objc private func onClickChangeButton(sender: UIButton) {
switch sender.tag {
case 0:
self._selectedNumber -= 1
if backEditNumber != nil {
backEditNumber!(false)
}
case 1:
self._selectedNumber += 1
if backEditNumber != nil {
backEditNumber!(true)
}
default:
break
}
}
使用方法:
private lazy var selectNumberView: EWSelectNumberView = {
let view = EWSelectNumberView(frame: CGRect(x: 50, y: 200, width: 96, height: 24), defaultNumber: 99, maxNumber: 100)
view.backEditNumber = { bool in
var operation: String = bool == false ? "減" : "加"
EWToast.showBottomWithText(text: "1號(hào):\(operation)")
self.showLabel1.text = "\(view.selectedNumber)"
}
return view
}()
加減號(hào)圖片我隨便找的,如果接入使用,可以自己修改為合適的圖片.
demo地址:EWSelectNumberView
有問(wèn)題歡迎探討.