gpt4 book ai didi

ios - 添加自定义按钮后 UIScrollView 运行不流畅

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:43 26 4
gpt4 key购买 nike

我有一个 UIScrollView,其中有 500 多个自定义按钮,根据用户的要求,它最多可以达到 1000 个。实际上,是 UIButton 的子类。我使用 UIButton 类的原因是我必须在点击时执行一些操作。该对象看起来像下图

enter image description here

这些按钮的排列就像 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 矩阵。

提前致谢

最佳答案

我会查看 UICollectionView。你现在会遇到内存问题。 UICollectionView 允许您创建一个类似于 UITableViewCellUICollectionViewCell,可以重复使用。

参见 Apple Documentation .

关于ios - 添加自定义按钮后 UIScrollView 运行不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15940506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com