DuceShen / UIViewBlock

给UIView和UIButton加了一个带block参数的方法,便于添加事件

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UIViewBlock

使用效果

使用前:

  • UIView添加事件
-(void)xxx {  
    view.userInteractionEnabled = YES;  
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething:)];  
    [view addGestureRecognizer:tapGesture];  
}  

-(void)doSomething {
    ...
}
  • UIButton添加事件
-(void)xxx {  
    [btn addTarget:self action:@selector(doSomething:)forControlEvents:UIControlEventTouchUpInside];  
}  

-(void)doSomething {  
    ...  
}

使用后:

  • UIVIew添加事件
-(void)xxx {  
    [view addTap:^(UITapGestureRecognizer *tap) {  
        ...  
    }];  
}
  • UIButton添加事件
-(void)xxx {  
    [btn addTap:^(UIButton *btn) {  
        ...  
    }];  
}

使用方法

直接拖入工程,或用cocoaPod导入

使用建议

建议配合以下两个宏使用(借鉴自yykit),防止循环引用

#ifndef weakify
#if DEBUG
#if __has_feature(objc_arc)
#define weakify(object) autoreleasepool{} __weak typeof(object) weak####object = object;
#else
#define weakify(object) autoreleasepool{} __block typeof(object) block##
##object = object;
#endif
#else
#if __has_feature(objc_arc)
#define weakify(object) try{} @finally{} {} __weak typeof(object) weak####object = object;
#else
#define weakify(object) try{} @finally{} {} __block typeof(object) block##
##object = object;
#endif
#endif
#endif
#ifndef strongify
#if DEBUG
#if __has_feature(objc_arc)
#define strongify(object) autoreleasepool{} typeof(object) object = weak####object;
#else
#define strongify(object) autoreleasepool{} typeof(object) object = block##
##object;
#endif
#else
#if __has_feature(objc_arc)
#define strongify(object) try{} @finally{} typeof(object) object = weak####object;
#else
#define strongify(object) try{} @finally{} typeof(object) object = block##
##object;
#endif
#endif
#endif

宏可放入.pch或Global.h等文件, 并且宏配合xcode自动完成效果更佳。
宏搭配使用效果:

-(void)xxx {  
    @weakify(self)  
    [btn addTap:^(UIButton *btn) {  
        @strongify(self)  
        ...  
    }];  
} 

About

给UIView和UIButton加了一个带block参数的方法,便于添加事件

License:MIT License


Languages

Language:Objective-C 78.2%Language:Ruby 21.8%