我有一个 UIScrollView,其中有 500 多个自定义按钮,根据用户的要求最多可以达到一千个。实际上,UIButton 的子类。我使用 UIButton 类的原因是我必须在点击时执行一些操作。对象如下图所示
这些按钮的排列就像 m*n 矩阵,其中行 (m) 固定为 20,但列 (n) 不固定。可以有 500 列。
我的问题是添加按钮后,scrollView 的行为不流畅。我使用以下代码来创建每个对象
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
sizeLabel = [[UILabel alloc] initWithFrame:sizeLabelRect];
sizeLabel.font = [UIFont boldSystemFontOfSize:sizeLabelFontSize];
sizeLabel.textAlignment = UITextAlignmentCenter;
// sizeLabel.backgroundColor = [self colorWithHexString:LABEL_BG_COLOR];
// sizeLabel.layer.cornerRadius = CORNER_RADIUS;
[self addSubview:sizeLabel];
nFilesLabel = [[UILabel alloc] initWithFrame:nFilesLabelRect];
nFilesLabel.font = [UIFont boldSystemFontOfSize:nFilesFontSize];
nFilesLabel.textAlignment = UITextAlignmentCenter;
// nFilesLabel.backgroundColor = [self colorWithHexString:LABEL_BG_COLOR];
// nFilesLabel.layer.cornerRadius = CORNER_RADIUS;
[self addSubview:nFilesLabel];
maxDisimilarity = [[UILabel alloc] initWithFrame:disimilarityRect];
// maxDisimilarity.layer.cornerRadius = CORNER_RADIUS;
[self addSubview:maxDisimilarity];
// nFragment = [[UILabel alloc] init];
// nFragment.font = [UIFont systemFontOfSize:nFragmentFontSize];
// [self addSubview:nFragment];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (void)setObj:(CloneClass *)obj
{
NSString *colorCode;
if (obj != nil) {
_obj = obj;
if ([_obj.type isEqualToString:@"1"]) {
colorCode = COLOR;
} else if ([_obj.type isEqualToString:@"2"]) {
colorCode = COLOR;
} else if ([_obj.obj isEqualToString:@"Type-3"]) {
colorCode = COLOR;
}
self.backgroundColor = [self colorWithHexString:colorCode];
[self setTitle:[NSString stringWithFormat:@"%d", obj.nFragments] forState:UIControlStateNormal];
// set size of clone class
if (_obj.size > LARGE_SIZE) {
sizeLabel.text = LARGE_SIZE_TEXT;
} else if (_obj.size > MEDIUM_SIZE && _obj.size <= LARGE_SIZE) {
sizeLabel.text = MEDIUM_SIZE_TEXT;
} else {
sizeLabel.text = SMALL_SIZE_TEXT;
}
// set number of files within a clone class
nFilesLabel.text = [NSString stringWithFormat:@"%d", _cloneClass.nFiles];
// set color for disimilarity
CGFloat similarity = 1.0 - _cloneClass.maxDisimilarity;
maxDisimilarity.backgroundColor = [UIColor colorWithRed:1.0 green:similarity blue:similarity alpha:1.0];
} else {
self.backgroundColor = [UIColor grayColor];
[self setTitle:@"/" forState:UIControlStateNormal];
}
self.titleLabel.textColor = [UIColor blackColor];
}
scrollView 甚至不适用于 10*10 矩阵。
提前致谢