ios - 有哪位大牛能把这段代码写完整啊,UIScrollView九宫格左右分页

 陈汉文爱_290 发布于 2022-10-31 12:02

UIScrollView九宫格从左到右分页,类似于QQ、微信的聊天界面底部大表情分页

每行4个,每页8个。

我现在只能算出第一页,后面的位置不知道怎么算啊,有点混淆了

我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.faces = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < 51; i ++) {
        [self.faces addObject:@(i)];
    }
    [self addScrollView];
}

- (void) addScrollView
{
    self.otherExpressionScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 150)];
    self.otherExpressionScrollView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.otherExpressionScrollView];
    self.otherExpressionScrollView.center = self.view.center;
    self.otherExpressionScrollView.contentSize = CGSizeMake(self.otherExpressionScrollView.bounds.size.width * (self.faces.count / 8), self.otherExpressionScrollView.bounds.size.height);
    self.otherExpressionScrollView.pagingEnabled = YES;
    [self initButtons];
}


- (void) initButtons
{
    CGFloat leftSpace = (self.otherExpressionScrollView.bounds.size.width - otherFaceSize * 4) / 5;
    CGFloat pageWidth = self.otherExpressionScrollView.bounds.size.width;
    NSArray *localFaces = [NSArray arrayWithArray:self.faces];
//    NSInteger pages = self.faces.count % 8 > 0 ? (self.faces.count / 8) + 1 : self.faces.count / 8;
    for (NSInteger i = 0; i < localFaces.count; i ++) {

//        NSDictionary *faceDict = localFaces[i];

        //        NSString *faceImgUrl = [faceDict objectForKey:@"url"];
        NSString *faceTitle = [NSString stringWithFormat:@"%ld", (long)i];//[faceDict objectForKey:@"title"];
        UIButton *faceButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [faceButton setTitle:faceTitle forState:UIControlStateNormal];
        faceButton.tintColor = [UIColor whiteColor];
        faceButton.backgroundColor = [UIColor lightTextColor];
        faceButton.alpha = 0.6;
        faceButton.contentMode = UIViewContentModeTop | UIViewContentModeBottom;
        faceButton.size = CGSizeMake(otherFaceSize, otherFaceSize);
        [self.otherExpressionScrollView addSubview:faceButton];
        CGFloat lineCount = 4;
        faceButton.left = (i % 4) * (otherFaceSize + leftSpace) + leftSpace;
        faceButton.top = (i / 4) * (otherFaceSize + leftSpace) + 2;
        faceButton.width = faceButton.height = otherFaceSize;


        if (i > 7) { //
            NSInteger page = i % 8 > 0 ? (i / 8) + 1 : i / 8;
            faceButton.top = ((i - 8) / 4) * (otherFaceSize + leftSpace) + 2 - page * 89;
            faceButton.left = ((i - 8) % 4) * (otherFaceSize + leftSpace) + leftSpace + pageWidth * page;
            NSLog(@"%f %f", faceButton.left, pageWidth * page);
        }

    }
}
3 个回答
  • 我会这样做 :

    1. 三重for
    for (int page = 0; page < 2; page ++)
        {
            for (int x = 0; x < 4; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    int i = x + 4 * y;
                    CGRect frame = CGRectZero;
                    frame.origin.y = y * 70;
                    frame.origin.x = x * 70 + 60 + page * self.view.frame.size.width;
                    frame.size = CGSizeMake(50, 50);
                    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
                    btn.frame = frame;
                    [btn setTitle:[@(i) stringValue] forState:UIControlStateNormal];
                    btn.backgroundColor = [UIColor lightGrayColor];
                    [self.scrollView addSubview:btn];
                }
            }
        }
        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    

    ##

    1. 一重for
    for (int i = 0; i < 16; i++)
        {
            CGRect frame = CGRectZero;
            frame.origin.y = i % 8 / 4 * 70;
            frame.origin.x = i % 4 * 70 + 60 + i / 8 * self.view.frame.size.width;
            frame.size = CGSizeMake(50, 50);
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
            btn.frame = frame;
            [btn setTitle:[@(i) stringValue] forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor lightGrayColor];
            [self.scrollView addSubview:btn];
    
        }
        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    
    

    ##

    还有用uicollectionview的

    2022-10-31 22:56 回答
  • 建议使用uicollectionview。

    2022-10-31 22:56 回答
  • https://github.com/yanyin1986/PagedHorizentalCollectionView
    我以前自己写的一个CollectionView,应该是你要的

    2022-10-31 22:57 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有