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

发布于 2022-09-01 16:07:48 字数 2865 浏览 34 评论 0

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);
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

于我来说 2022-09-08 16:07:48

https://github.com/yanyin1986/PagedHorizentalCollectionView
我以前自己写的一个CollectionView,应该是你要的

我纯我任性 2022-09-08 16:07:48

建议使用uicollectionview。

阪姬 2022-09-08 16:07:48

我会这样做 :

  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的

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文