WCBlock
- UIKit 扩展的轻量级块库,它会让您的代码更加简单,您会永远爱它
如何开始
手动导入
- 将 "WCBlock" 文件夹拖到您的项目中
- 导入主文件:
#import "WCBlock.h"
使用 CocoaPods 进行安装
source 'https://github.com/manakiaHk/WCBlock.git'
platform :ios, '7.0'
target 'TargetName' do
pod 'WCBlock'
end
然后,运行以下命令
$ pod install
用法
视图
///下面view的每个block 都将调用,他们的返回值都是同一个对象,因为你知道 ,每一个view只能同时绑定一个同样类型的手势.
WCViewTap *tap0 = [view wc_bindViewTapBlockNext:^(UIView *view, WCViewTap *tap) {
NSLog(@"0--view taped");
}];
[view wc_bindViewTapBlockNext:^(UIView *view, WCViewTap *tap) {
NSLog(@"1--view taped");
}];
[view wc_bindViewTapBlockNext:^(UIView *view, WCViewTap *tap) {
NSLog(@"2--view taped");
}];
[view wc_bindViewTapBlockNext:^(UIView *view, WCViewTap *tap) {
NSLog(@"3--view taped");
}];
///你可以通过返回值设置属性以及代理 这点和Apple的api完全一样 ,比如:
tap0.numberOfTapsRequired = 2;
tap0.delegate = self;
///你还可以绑定其他的手势block回调。e.g:
[view wc_bindViewPanBlockNext:^(UIView *view, WCViewPan *pan) {
NSLog(@"pan...");
}];
[view wc_bindViewLongPressBlockNext:^(UIView *view, WCViewLongPress *longPress) {
NSLog(@"longPressed");
}];
[view wc_bindViewRotationBlockNext:^(UIView *view, WCViewRotation *rotation) {
NSLog(@"%0.2f",rotation.rotation);//旋转角度
NSLog(@"%0.2f",rotation.velocity);//旋转速度
}];
手势识别器
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]init];
[tapGes wc_bindGestureBlockNext:^(UIGestureRecognizer *sender) {
//...
}];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]init];
[swipeGesture wc_bindGestureBlockNext:^(UIGestureRecognizer *sender) {
//...
}];
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]init];
[rotationGesture wc_bindGestureBlockNext:^(UIGestureRecognizer *sender) {
//...
}];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]init];
[panGesture wc_bindGestureBlockNext:^(UIGestureRecognizer *sender) {
//...
}];
// and so on ...
按钮、分段控制器、滑块等。例如
UIButton *button = [[UIButton alloc]initWithFrame:btnFrame];
[button wc_bindForControlEvents:UIControlEventTouchUpInside blockNext:^(id sender) {
//button clicked...
}];
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"title0",@"title1",@"title2"]];
[segment wc_bindSegmentControlValueChangedBlockNext:^(NSInteger selectedIndex) {
NSLog(@"segment selected index %ld",selectedIndex);
}];
//tip: 和以往一样,当wcblock 捕获了外部变量,可能将导致循环引用 ,你需要用 __weak 避免这样事情发生
UISlider *slider = [[UISlider alloc]initWithFrame:sliderFrame];
__weak typeof(self) weakSelf = self;
[slider wc_bindSliderValueChangedBlockNext:^(CGFloat value) {
__strong typeof(weakSelf) self = weakSelf;
[self sendAMesseage]
}];
UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancle" otherButtonTitles:@"ok", nil];
[alerView wc_bindAlertButtonClickedBlockNext:^(NSInteger index) {
NSLog(@"clicked index: %ld",index);
}];
[alerView show];
文本框
UITextField *textfiled = [[UITextField alloc]initWithFrame:textFieldframe];
[textfiled wc_bindTextFieldEditingChangedBlockNext:^(UITextField *textField, NSString *value) {
NSLog(@"textfiled text:%@",value);
}];
[textfiled wc_bindTextFieldShouldChangeCharactersHandlerBlock:^BOOL(UITextField *textField, NSRange shouldChangeCharactersInRange, NSString *replacementString) {
return YES;
}];
[textfiled wc_bindTextFieldEditingDidBeginBlockNext:^(UITextField *textField) {
//textfiled did begin editing...
}];
[textfiled wc_bindTextFieldEditingDidEndBlockNext:^(UITextField *textField) {
//textfiled did end editing...
}];
通知中心,WCBlock 将为您自动管理移除消息中心的 observer 对象
[[NSNotificationCenter defaultCenter] wc_addObserverForName:@"wc_noti_demo" object:nil contextObj:self blockNext:^(NSNotification * _Nullable note) {
NSLog(@"%@",note.userInfo[@"note_demo"]);
}];
///notification test demo
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"wc_noti_demo" object:nil userInfo:@{@"note_demo":@"WCBlock将自动为你管理移除observer对象"}];
});
KVO
[self.label wc_addObserverForKeyPath:@"text" valueBlockNext:^(NSString *keypath, id ofObj, id oldValue, id newValue) {
NSLog(@"label.text = %@",newValue);
}];
[self.label wc_addObserverForKeyPaths:@[@"alpha",@"text"] valueBlockNext:^(NSString *keypath, id ofObj, id oldValue, id newValue) {
if ([keypath isEqualToString:@"alpha"]) {
NSLog(@"label.alpha= %@",newValue);
}else if([keypath isEqualToString:@"text"]){
NSLog(@"label.text= %@",newValue);
}else;
}];
[self.label wc_addObserverForKeyPath:@"text" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld changeBlockNext:^(NSString *keypath, id ofObj, NSDictionary<NSKeyValueChangeKey,id> *change) {
///..
}];
和 Apple API 一样,对于 KVO,您需要自己移除键值观察,例如
- (void)dealloc {
[self.label wc_removeObserverForKeyPath:@"text"];
[self.label wc_removeObserverForKeyPath:@"alpha"];
//also
[self.label wc_removeObserverForKeyPaths:@[@"alpha",@"text"]];
}
提示:您可以为每个对象绑定多个同样类型的 block,每个 block 都会调用,因为您可能会在多个地方同时使用,所以您需要知道 WCBlock 可以做到这一点。但是记住 handlerBlock 只能绑定一个,因为您不希望多个 handler 同时操作一个对象,所以对于 handlerBlock,绑定多个同样类型的 block,WCBlock 是不允许的,此时只有最后一个有效。例如:
[view wc_bindViewTapBlockNext:^(UIView *view, WCViewTap *tap) {
NSLog(@"0--view taped");
}];
[view wc_bindViewTapBlockNext:^(UIView *view, WCViewTap *tap) {
NSLog(@"1--view taped");
}];
[view wc_bindViewTapBlockNext:^(UIView *view, WCViewTap *tap) {
NSLog(@"2--view taped");
}];
[textfiled wc_bindTextFieldEditingChangedBlockNext:^(UITextField *textField, NSString *value) {
NSLog(@"0--textfiled text:%@",value);
}];
[textfiled wc_bindTextFieldEditingChangedBlockNext:^(UITextField *textField, NSString *value) {
NSLog(@"1--textfiled text:%@",value);
}];
[textfiled wc_bindTextFieldEditingChangedBlockNext:^(UITextField *textField, NSString *value) {
NSLog(@"2--textfiled text:%@",value);
}];
下面的 block 只有最后一个有效(请注意,它们是 HandlerBlock)
[textfiled wc_bindTextFieldShouldChangeCharactersHandlerBlock:^BOOL(UITextField *textField, NSRange shouldChangeCharactersInRange, NSString *replacementString) {
if ([replacementString containsString:@"a"]) {
return NO;
}
return YES;
}];
[textfiled wc_bindTextFieldShouldChangeCharactersHandlerBlock:^BOOL(UITextField *textField, NSRange shouldChangeCharactersInRange, NSString *replacementString) {
if ([replacementString containsString:@"b"]) {
return NO;
}
return YES;
}];
[textfiled wc_bindTextFieldShouldChangeCharactersHandlerBlock:^BOOL(UITextField *textField, NSRange shouldChangeCharactersInRange, NSString *replacementString) {
if ([replacementString containsString:@"c"]) {
return NO;
}
return YES;
}];