scrollview技巧

时间:2023-10-13 10:17:02

一行

    CGFloat buttonX = 0;
CGFloat buttonY = 25;
CGFloat buttonW = 37;
CGFloat buttonH = 60;
CGFloat margin = (self.view.width - 6 * buttonW) / 7;
for (int i = 0; i < self.countryArr.count; i++) {
YTBottomTitleButton *button = self.countryArr[i];
buttonX = margin + (buttonW + margin) * i;
button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
}
self.countryScrollView.contentSize = CGSizeMake(self.countryArr.count * (buttonW + margin) + margin, 0);

两行

    buttonX = 0;
buttonY = 25;
buttonW = 50;
buttonH = 20;
NSInteger viewCount = 5;//一行view数量
margin = (self.view.width - viewCount * buttonW) / 6;//左右间距
CGFloat ymargin = 17;//上下间距
NSInteger pageViewCount = 0;//一页view数量
if (self.view.width > 320) {//iPhone6 or later
pageViewCount = 20;
}
else {
pageViewCount = 15;
}
for (int i = 0; i < self.platformArr.count; i++) {
YTBottomTitleButton *button = self.platformArr[i];
buttonX = margin + (i % viewCount == 0 ? 0 : (buttonW + margin) * (i % viewCount)) + (i % pageViewCount == 0 ? ZWScreenW * i / pageViewCount : ZWScreenW * (i - i % pageViewCount) / pageViewCount);
buttonY = i % viewCount == 0 ? (i % pageViewCount == 0 ? ymargin : ymargin + (buttonH + ymargin) * ((i % pageViewCount) / viewCount)) : buttonY; button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
} NSInteger offset = self.platformArr.count > pageViewCount ? (self.platformArr.count % pageViewCount == 0 ? self.platformArr.count / 20 : self.platformArr.count / pageViewCount + 1) : 0;
self.platformScrollView.contentSize = CGSizeMake(offset * ZWScreenW, 0);