一、自定义工具条
1、使用。
YBImageBrowser *browser = [YBImageBrowser new];
browser.toolViewHandlers = @[YXToolViewHandler.new];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageBrowser_deletNoti:) name:@"YXImageBrowser_delet_noti" object:nil];
- (void)imageBrowser_deletNoti:(NSNotification *)nofi {
NSInteger curPage = [nofi.object[@"curPage"] integerValue];
NSInteger totalPage = [nofi.object[@"totalPage"] integerValue];
if (totalPage<=1) {
[_browser hide];
return;
}
if (curPage<0) {
curPage = 0;
}else if (curPage>totalPage) {
curPage = totalPage;
}
YXPhotoData *data = nofi.object[@"data"];
NSMutableArray *tmp = [NSMutableArray arrayWithArray:self.browser.dataSourceArray];
[tmp removeObject:data];
_browser.dataSourceArray = tmp;
_browser.currentPage = curPage;
[_browser reloadData];
}
2、实现,
#import "YBIBToolViewHandler.h"
@interface YXToolViewHandler : NSObject<YBIBToolViewHandler>
@end
#import "YXToolViewHandler.h"
#import "YXPhotoData.h"
@interface YXToolViewHandler()
@property (nonatomic, strong) UIButton *deletBtn;
@property (nonatomic, strong) UILabel *pageLabel;
@end
@implementation YXToolViewHandler
- (void)deletBtnAction:(UIButton *)btn {
YXPhotoData *data = self.yb_currentData();
NSInteger curPage = self.yb_currentPage();
NSInteger totalPage = self.yb_totalPage();
NSDictionary *dict = @{ @"data": data, @"curPage": @(curPage), @"totalPage": @(totalPage) };
[[NSNotificationCenter defaultCenter] postNotificationName:@"YXImageBrowser_delet_noti" object:dict];
}
#pragma mark - <YBIBToolViewHandler>
@synthesize yb_containerView = _yb_containerView;
@synthesize yb_currentData = _yb_currentData;
@synthesize yb_containerSize = _yb_containerSize;
@synthesize yb_currentOrientation = _yb_currentOrientation;
@synthesize yb_currentPage = _yb_currentPage;
@synthesize yb_totalPage = _yb_totalPage;
- (void)yb_containerViewIsReadied {
CGSize size = self.yb_containerSize(self.yb_currentOrientation());
[self.yb_containerView addSubview:self.deletBtn];
[self.yb_containerView addSubview:self.pageLabel];
self.pageLabel.frame = CGRectMake(20, 20, 60, 40);
self.deletBtn.frame = CGRectMake(size.width-100, 20, 60, 40);
}
- (void)yb_hide:(BOOL)hide {
YXPhotoData *data = self.yb_currentData();
// NSLog(@"%@",data.model.imgUrl);
}
- (void)yb_pageChanged {
// 拿到当前的数据对象(此案例都是图片)
// YXPhotoData *data = self.yb_currentData();
// NSLog(@"%@",data.model.imgUrl);
NSInteger curPage = self.yb_currentPage();
NSInteger totalPage = self.yb_totalPage();
self.pageLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)curPage+1, (long)totalPage];
}
- (void)yb_orientationChangeAnimationWithExpectOrientation:(UIDeviceOrientation)orientation {
// 旋转的效果自行处理了
}
#pragma mark - lazy
- (UIButton *)deletBtn {
if (!_deletBtn) {
_deletBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_deletBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
[_deletBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
_deletBtn.backgroundColor = [UIColor.grayColor colorWithAlphaComponent:0.75];
_deletBtn.layer.cornerRadius = 5.0;
[_deletBtn setTitle:@"删除" forState:UIControlStateNormal];
[_deletBtn addTarget:self action:@selector(deletBtnAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _deletBtn;
}
- (UILabel *)pageLabel {
if (!_pageLabel) {
_pageLabel = [[UILabel alloc] init];
_pageLabel.font = [UIFont boldSystemFontOfSize:15];
_pageLabel.textColor = [UIColor whiteColor];
_pageLabel.textAlignment = NSTextAlignmentCenter;
}
return _pageLabel;
}
@end
二、cell自定义。
#import "YBIBDataProtocol.h"
#import "YXBrowseModel.h"
@interface YXPhotoData : NSObject<YBIBDataProtocol>
@property (nonatomic, strong) YXBrowseModel *model;
@end
#import "YXPhotoCell.h"
@implementation YXPhotoData
- (Class)yb_classOfCell {
return YXPhotoCell.self;
}
@end
#import "YBIBCellProtocol.h"
@interface YXPhotoCell : UICollectionViewCell<YBIBCellProtocol>
@end
#import "YXPhotoCell.h"
#import "YXPhotoData.h"
@interface YXPhotoCell()
@property (nonatomic, strong) UIImageView *imgView;
@end
@implementation YXPhotoCell
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
self.contentView.backgroundColor = [UIColor blackColor];
[self.contentView addSubview:self.imgView];
[self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(0);
}];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
self.yb_hideBrowser();
}
- (void)prepareForReuse {
[super prepareForReuse];
}
#pragma mark - <YBIBCellProtocol>
@synthesize yb_cellData = _yb_cellData;
@synthesize yb_hideBrowser = _yb_hideBrowser;
@synthesize yb_cellIsInCenter = _yb_cellIsInCenter;
@synthesize yb_currentPage = _yb_currentPage;
@synthesize yb_selfPage = _yb_selfPage;
- (void)setYb_cellData:(id<YBIBDataProtocol>)yb_cellData {
_yb_cellData = yb_cellData;
YXPhotoData *data = (YXPhotoData *)yb_cellData;
YXBrowseModel *model = data.model;
[self.imgView sd_setImageWithURL:[NSURL URLWithString:model.imgUrl]];
}
- (void)yb_pageChanged {
if (self.yb_currentPage() != self.yb_selfPage()) {
} else {
}
}
#pragma mark - lazy
- (UIImageView *)imgView {
if (!_imgView) {
_imgView = [[UIImageView alloc] init];
_imgView.contentMode = UIViewContentModeCenter;
}
return _imgView;
}
@end