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

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

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

    }
}

建议使用uicollectionview。


我会这样做 :

  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的


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

【热门文章】
【热门文章】