#import <UIKit/UIKit.h>
typedef void(^BtnHandleAction)(NSString *leftOrRight);
@interface CollectionSectionView : UICollectionReusableView
@property (nonatomic,copy)NSString *leftStr;
@property (nonatomic,copy)NSString *rightStr;
- (void)customBtnHandelAction:(BtnHandleAction)action;
@end
#import "CollectionSectionView.h"
@interface CollectionSectionView ()
@property (nonatomic,strong)UIButton *leftBtn;
@property (nonatomic,strong)UIButton *rightBtn;
@property (nonatomic,copy)BtnHandleAction btnHandelAction;
@end
@implementation CollectionSectionView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (UIButton *)leftBtn{
if (!_leftBtn) {
self.leftBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
_leftBtn.tag = 2000;
_leftBtn.frame = CGRectMake(1, 1, self.frame.size.width / 2 - 2, self.frame.size.height - 2);
_leftBtn.backgroundColor = [UIColor whiteColor];
[_leftBtn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}
return _leftBtn;
}
- (UIButton *)rightBtn{
if (!_rightBtn) {
self.rightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
_rightBtn.tag = 2001;
_rightBtn.frame = CGRectMake(self.frame.size.width / 2 + 1, 1, self.frame.size.width / 2 - 2, self.frame.size.height - 2);
_rightBtn.backgroundColor = [UIColor whiteColor];
[_rightBtn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}
return _rightBtn;
}
- (void)btnAction:(UIButton *)sender{
NSInteger tag = sender.tag;
if (tag == 2000) {
self.btnHandelAction(@"left");
}
if (tag == 2001) {
self.btnHandelAction(@"right");
}
}
- (void)customBtnHandelAction:(BtnHandleAction)action{
self.btnHandelAction = action;
}
- (void)setup{
[self addSubview:self.leftBtn];
[self addSubview:self.rightBtn];
}
- (void)setLeftStr:(NSString *)leftStr{
[self.leftBtn setTitle:leftStr forState:(UIControlStateNormal)];
[self.leftBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
}
- (void)setRightStr:(NSString *)rightStr{
[self.rightBtn setTitle:rightStr forState:(UIControlStateNormal)];
[self.rightBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
}
@end
总结
以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能有所帮助,如果有疑问大家可以留言交流。
注:相关教程知识阅读请移步到IOS开发频道。










