2里面写了一对一的,因为在Response里面只声明一个响应者(block),想要一对多就需要一个队列了,
加一个array就行了吧。
上代码试试看:
var re = Response()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.white
let label = RLabel()
label.backgroundColor = UIColor.blue
label.textColor = UIColor.red
label.frame = CGRect.init(x: 50, y: 80, width: 80, height: 30)
self.view.addSubview(label)
let label1 = RLabel()
label1.backgroundColor = UIColor.blue
label1.textColor = UIColor.red
label1.frame = CGRect.init(x: 200, y: 80, width: 80, height: 30)
self.view.addSubview(label1)
let button = UIButton()
button.setTitle("button", for: .normal)
button.setTitleColor(UIColor.black, for: .normal)
button.addTarget(self, action: #selector(addText), for: .touchUpInside)
self.view.addSubview(button)
button.frame = CGRect.init(x: 50, y: 180, width: 80, height: 30)
label.addResponse(re: re)
label1.addResponse(re: re)
print(re[8])
}
@objc func addText(){
re.text = "wyy_click"
}
}
class RLabel:UILabel{
func addResponse(re:Response){
re.responseContent({[weak self] in
self?.text = re.text
})
}
}
typealias responseBlock = () -> ()
class Response:NSObject{
//响应者队列
var array = Array<responseBlock>()
private var _text:String?
var text:String?{
set{
_text = newValue
sendReponserMsg()
}
get{
return _text
}
}
func responseContent(_ callBack:@escaping responseBlock){
array.append(callBack)
}
//触发响应
func sendReponserMsg(){
for value in array{
value()
}
}
//下标
subscript(index:Int) -> String?{
return "sub"
}
//运算符重载
static func + (a:Response,b:Response) -> Response{
let c = Response()
c.text = a.text! + b.text!
return c
}
}
还不错,简简单单的就把功能实现了。
但是仍然无法运用的到编程中,还不够完善因为现在只有一个RLable和Response(text)对象,
继续丰富他吧。
Vue非常的流行,我也非常喜欢。就照着Vue的api来完整他吧。(对html不是很精通,有不对的还请指教,多谢)
网友评论