• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:23:05 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap