实现效果是,循环子view,给符合条件的子view进行操作。
原来的实现:
for view in subviews {
if view is UIButton {
let btn: UIButton = view as! UIButton
if btn.tag <= level {
btn.isSelected = true
}else{
btn.isSelected = false
}
}
}
改进后的实现:
subviews.forEach { (view) in
(view as? UIButton)?.isSelected = view.tag <= level
}
简单了很多,但是可读性也增加了不少。各有利弊吧。