CSLCommonLibrary
一些常用工具库,基于基础库封装,方便调用。
NSObject+Base
处理了targe的dealloc方法,方便这个库中其他的类对kvo,notification,timer的手动移除。 - (void)swizzMethod:(NSObject *)target action:(SwizzActionType)type callback:(void(^)(__unsafe_unretained NSObject *deallocObj))callback; 处理了targe的viewWillDisappear:方法,方便这个库中其他的类对kvo,notification,timer的手动移除。当然调用者也可调用这两个方法,例如对delegate进行回调等。
SLTimer
继承NSTimer,内部自动处理invalidate方法。也自动防止循环引用问题,避免手动置nil的操作。当然调用者也可手动对SLTimer对象调用invalidate方法来停止计时器。 + (instancetype)sl_timerWithTimeInterval:(NSTimeInterval)timeInterval target:(id)aTarget userInfo:(nullable id)userInfo repeats:(BOOL)repeat mode:(NSRunLoopMode)mode callback:(void(^)(NSArray *array))timerCallback
NSNotificationCenter+Base
解决了notification的手动移除问题 使用- (void)addTarget:(NSObject *)target noidtificationName:(NSString *)notificationName object:(id)object block:(NotificationReturnBlock)block即可
BaseObserver
解决了kvo的手动移除问题。使用- (instancetype)initWithTarget:(NSObject *)target keyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options block:(ObserverReturnBlock)block即可。
CSLDelegateProxy
解决了delegate需要weak引用的问题,现在你可以放心地用strong去修饰你的delegate。同时在你的类中不需要实现任何协议和协议对应的方法。1、将你需要实现的协议名称传递过去 - (instancetype)initWithDelegateProxy:(Protocol *)protocol 2、添加你需要实现的协议方法名,例如(alertView:clickedButtonAtIndex:),block回调中的参数包括协议调用方所传递的所有参数。当然,这个类使用的场景多半是自定义协议 - (void)addSelector:(SEL)selector callback:(void(^)(id))callback
对于系统的常见组件。
UIAlertView+DelegateProxy
例如UIAlertView,它的delegate已经进行了封装,所以使用UIAlertView时不需要对delegate进行赋值。例如:[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil] 其中- (void)buttonClicked:(void (^)(UIAlertView *actionView, int clickIndex))clickBlock就提供了UIAlertView的点击回调。
UITextView+DelegateProxy
封装了UITextView的全部代理回调,自动设置代理。代理方法太多,这里不进行举例。
UIControl+Events
除了协议之外,所有继承自UIControl的触摸响应组件,我们可以使用 - (void)onEventChange:(NSObject *)target event:(UIControlEvents)event change:(void (^)(UIControl *))changeBlock 传递你需要监听的触摸事件UIControlEvents,在block回调中就会拿到你需要的组件对应的值。
CAAnimation+DelegateProxy
对CAAnimation的两个回调进行了封装处理。自动设置delegate - (void)animationDidStartBlock:(void (^)(CAAnimation *anim))animationDidStartBlock; - (void)animationDidStopBlock:(void (^)(CAAnimation *animm, BOOL finished))animationDidStopBlock;
UITableViewHeaderFooterView+DelegateProxy
UITableViewCell+DelegateProxy
UICollectionReusableView+DelegateProxy
- (void)reusableCallback:(void (^)(id))callback 表示即将重用的回调。如果平时有用他的prepareForReuse方法,那么这里就可以替换成上面的方法了。
整个库的整体结构都包含了block回调,这时候你的代码看上去会比较紧凑。更加容易分析。当然记得不要有循环引用的问题 __weak 和 __strong很有必要。
更多基础库的封装大家可以去我的代码中查找,后续我会继续完善这里的东西。