OGeek|极客世界-中国程序员成长平台

标题: ios - 添加自定义按钮后,UIScrollView 行为不流畅 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:23
标题: ios - 添加自定义按钮后,UIScrollView 行为不流畅

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

enter image description here

这些按钮的排列类似于 m*n 矩阵,其中行 (m) 固定为 20,但列 (n) 不固定。可以有 500 列。

我的问题是添加按钮后,scrollView 的行为不流畅。我使用以下代码创建每个对象

- (id)initWithFrameCGRect)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 colorWithHexStringABEL_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 colorWithHexStringABEL_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)drawRectCGRect)rect
{
    // Drawing code
}
*/

- (void)setObjCloneClass *)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 矩阵。

提前致谢



Best Answer-推荐答案


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

Apple Documentation .

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4