有哪位大牛能把这段代码写完整啊,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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
https://github.com/yanyin1986/PagedHorizentalCollectionView
我以前自己写的一个CollectionView,应该是你要的
建议使用uicollectionview。
我会这样做 :
##
##
还有用uicollectionview的