//
// OPSudokuView.m
// OPSudokuView
//
// Created by liuhangjun on 2021/8/17.
//
#import "OPSudokuView.h"
@interface OPSudokuView()
@property (nonatomic, strong) NSMutableArray *btnArr;//存放ViewModel的数组
@property (nonatomic, assign) UIScrollView * contView;
@end
@implementation OPSudokuView
-(instancetype)initWithArray:(NSArray*)array{
if(self= [superinit]) {
[selfsetDefault];
self.dataArr= array;
}
return self;
}
- (void)setDefault{
self.cWidth=50;
self.cHeight=50;
self.cEstimateHMarign = 10;
self.cEstimateVMarign = 10;
self.cRealMargin = NO;
self.scrollDirection = OPScrollDirectionVertical;
[self setUpView];
}
-(instancetype)initWithFrame:(CGRect)frame{
if(self= [superinitWithFrame:frame]) {
[selfsetDefault];
[selfsetUpView];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)framewithArray:(NSArray*)array{
if(self= [selfinitWithFrame:frame]) {
self.dataArr = array;
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
[self layoutContentV];
}
- (void)layoutContentV{
CGFloatwidth =self.bounds.size.width;
CGFloatheight =self.bounds.size.height;
//横竖屏适配需要在此重设frame或约束
NSIntegercount =self.dataArr.count;//子视图数量
CGFloatbuttonW =self.cWidth;//宽度
CGFloatbuttonH =self.cHeight;//高度
NSIntegerestimateHmargin =self.cEstimateHMarign;//间距
NSIntegerestimateVmargin =self.cEstimateVMarign;//间距
intcol =0;//列
CGFloatlineWidth = buttonW+estimateHmargin;
while(lineWidth
lineWidth += buttonW+estimateHmargin;
col++;
NSLog(@"%lf,%d", lineWidth, col);
}
//计算需要几行显示
intline = (int)((count+col-1)/col);
CGFloatrealmargin = (width - ( buttonW * col)) / (col +1);
//----------------------------------------------------------------------------------------
// UIInterfaceOrientation oritentation = [[UIApplication sharedApplication] statusBarOrientation];
// BOOL isPortrait ;
// if (UIInterfaceOrientationIsPortrait(oritentation)) {
// isPortrait = YES;
// }
//else{
// isPortrait = NO;
//}
//设置显示区域的frame
CGFloatsX;
CGFloatsY;
CGFloatsW;
CGFloatsH;
//内容真实所需高度
CGFloatcontRealH =( estimateVmargin + buttonH ) * line + estimateVmargin;
//高度差
CGFloatdistanceH = height - contRealH;
//高度是否足够
BOOLcanShowALL = distanceH >0;
self.contentMode = OPSudokuOPContentModeBottom;
if (self.contentMode == OPContentModeCenter) {
sX =0;
sY = canShowALL ? (distanceH /2) :0;
sW = width;
sH = canShowALL ? contRealH : height;
}else if(self.contentMode == OPSudokuOPContentModeTop){
sX =0;
sY =0;
sW = width;
sH = canShowALL ? contRealH : height;
}else{
sX =0;
sY = canShowALL ? (distanceH) :0;
sW = width;
sH = canShowALL ? contRealH : height;
}
self.contView.frame= CGRectMake(sX, sY, sW, sH);
for(inti =0; i
///
NSIntegerrow_index = i/col;
NSIntegercol_index = i%col;
CGFloatbtnX = realmargin + (buttonW + realmargin) * col_index;
CGFloatbtnY = estimateVmargin + (buttonH + estimateVmargin) * row_index;
UIButton*btn =self.btnArr[i];
btn.frame=CGRectMake(btnX, btnY, buttonW, buttonH);
btn.backgroundColor = UIColor.yellowColor;
}
self.contView.contentSize=CGSizeMake(sW, contRealH);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@",self.contView);
});
}
-(void)setDataArr:(NSArray*)dataArr{
_dataArr= dataArr;
NSArray*array =self.dataArr;
for(inti =0; i
UIView*btn = array[i];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
[btnaddGestureRecognizer:tap];
btn.tag= i;
// [btn setData:data];
[_contViewaddSubview:btn];
[self.btnArraddObject:btn];
}
}
- (void)click:(UITapGestureRecognizer *)tap{
if(self.delegate && [self.delegaterespondsToSelector:@selector(sudokuView:didSelectIndex:)]) {
[self.delegate sudokuView:self didSelectIndex:1];
}
}
- (void)setUpView {
UIScrollView *contView = [[UIScrollView alloc]init];
[selfaddSubview:contView];
self.contView= contView;
contView.backgroundColor=UIColor.redColor;
}
#pragma mark --------- lazy -----------
-(NSMutableArray *)btnArr{
if(!_btnArr) {
_btnArr= [NSMutableArrayarray];
}
return _btnArr;
}
@end