1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// ICRFunctionBaseView.m
// XFFruit
//
// Created by Lili Wang on 15/4/1.
// Copyright (c) 2015年 Xummer. All rights reserved.
//
#define FUNCTION_COUNT_EACH_ROW (3)
#define MODULE_VIEW_WIDTH IBT_MAIN_SCREEN_WIDTH/FUNCTION_COUNT_EACH_ROW
#define HEIGHT_WIDTH_PROPORTION 112.0/105.0
#define MODULE_VIEW_HEIGHT MODULE_VIEW_WIDTH * HEIGHT_WIDTH_PROPORTION
#import "ICRFunctionBaseView.h"
#import "ICRFunctionEntity.h"
@interface ICRFunctionBaseView()
@property (nonatomic, assign) NSUInteger m_rows;
@property (nonatomic, assign) NSUInteger m_imageCountsOfLastRow;
@property (nonatomic, strong) NSArray *m_arrFunctions;
@end
@implementation ICRFunctionBaseView
#pragma mark - life Cycle
- (id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// code
}
return self;
}
#pragma mark - Public Method
+ (ICRFunctionBaseView *)initWithFunctionData:(NSArray *)arrFunctions {
ICRFunctionBaseView *baseView = [[ICRFunctionBaseView alloc] init];
baseView.m_rows = [baseView calculateShowRows:arrFunctions];
baseView.m_imageCountsOfLastRow = [baseView calculateRemainderLastRow:arrFunctions];
NSUInteger count = FUNCTION_COUNT_EACH_ROW;
for (int i = 0; i < baseView.m_rows; i ++) {
if (i == baseView.m_rows - 1 && baseView.m_imageCountsOfLastRow > 0) {
count = baseView.m_imageCountsOfLastRow;
}
for (int j = 0; j < count; j ++) {
int indexValue = FUNCTION_COUNT_EACH_ROW * i + j;
ICRFunctionEntity *functionEntity = [arrFunctions objectAtIndex:indexValue];
UIImage *functionImage = [UIImage imageNamed:functionEntity.iconName];
NSString *functionLabelTitle = functionEntity.functionName;
ICRFunctionItemControl *fControl = [ICRFunctionItemControl highLightControl];
fControl.frame = (CGRect){
.origin.x = MODULE_VIEW_WIDTH * j,
.origin.y = MODULE_VIEW_HEIGHT * i,
.size.width = MODULE_VIEW_WIDTH,
.size.height = MODULE_VIEW_HEIGHT
};
fControl.functinNameLabel.text = functionLabelTitle;
fControl.functionImage.image = functionImage;
fControl.tag = functionEntity.functionItemTag;
[fControl addTarget:baseView action:@selector(singleImageClike:) forControlEvents:UIControlEventTouchUpInside];
[baseView addSubview:fControl];
}
}
baseView.height = MODULE_VIEW_HEIGHT * baseView.m_rows;
return baseView;
}
#pragma mark - Private Method
- (NSUInteger)calculateShowRows:(NSArray *)arrImages {
NSUInteger remainder = arrImages.count % FUNCTION_COUNT_EACH_ROW;
NSUInteger rows = arrImages.count / FUNCTION_COUNT_EACH_ROW;
rows = remainder > 0 ? rows + 1 : rows;
return rows;
}
- (NSUInteger)calculateRemainderLastRow:(NSArray *)arrImages {
return arrImages.count % FUNCTION_COUNT_EACH_ROW;
}
#pragma mark - ICRFuctionItemControlDelegate
- (void)singleImageClike:(ICRFunctionItemControl *)imageView {
if ([self.m_delegate respondsToSelector:@selector(ICRFunctionBaseView:)]) {
[self.m_delegate ICRFunctionBaseView:imageView];
}
}
@end