类似 jQuery 风格将块处理程序附加到 UIControl 对象!
在您的 Podfile 中添加以下行:
pod "MTControl"
pod? => https://github.com/CocoaPods/CocoaPods/
注意:如果 pods 库只包含类别,您可能需要在目标构建设置中添加 "-all_load" 到 "其他链接器标志"。
简单的触摸事件
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[button touchDown:^(UIEvent *event) {
[_spinner start];
[_model fetchFromServer:@"http://mysterioustrousers.com" success:^(BOOL success){
[_spinner stop];
}];
}];
看?就像 jQuery!它与任何继承自 UIControl 的子类都兼容。
让我们尝试另一个
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
[textField editingDidBegin:^(UIEvent *event) {
textField.text = @"";
}];
要移除块(解绑):
[button removeBlocksForControlEvents:UIControlEventTouchDown];
touchDown
touchDownRepeat
touchDragInside
touchDragOutside
touchDragEnter
touchDragExit
touchUpInside
touchUpOutside
touchCancel
valueChanged
editingDidBegin
editingChanged
editingDidEnd
editingDidEndOnExit
allTouchEvents
allEditingEvents