test01和test02运行效果
test03运行效果
#import "ViewController.h"
#define ScrWidth [UIScreen mainScreen].bounds.size.width
#define ScrHeigh [UIScreen mainScreen].bounds.size.heigh
@interface ViewController ()
@property(nonatomic, strong) UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.scrollView];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(ScrWidth * 5, 0);
[self test1];
}
- (void)test1 {
NSInteger page = 0;
for (NSInteger i = 0 ; i < 67; i++) {
CGFloat btnW = (ScrWidth - 60 ) / 3.0;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:[NSString stringWithFormat:@"%ld", i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.layer.borderWidth = 1.0;
btn.layer.cornerRadius = 5.0;
btn.backgroundColor = [UIColor whiteColor];
[self.scrollView addSubview:btn];
if (i % 15 == 0 && i > 0) {
page ++ ;
}
CGFloat btnX = i % 3 * (btnW +20) + 10 + (page * ScrWidth);
CGFloat btnY = (i / 3)* 35 +12.5 - (page * 175);
btn.frame = CGRectMake( btnX, btnY, btnW, 30);
}
}
- (void)test2{
NSInteger page = 0;
NSInteger row = 0;
for (NSInteger i = 0 ; i < 25; i++) {
CGFloat btnW = (ScrWidth - 60 ) / 3.0;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:[NSString stringWithFormat:@"%ld", i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.layer.borderWidth = 1.0;
btn.layer.cornerRadius = 5.0;
btn.backgroundColor = [UIColor whiteColor];
[self.scrollView addSubview:btn];
if (i % 15 == 0 && i > 0) {
page ++ ;
row = 0 ;
}
CGFloat btnX = i % 3 * (btnW +20) + 10 + (page * ScrWidth);
CGFloat btnY = (row / 3)* 35 +12.5;
row ++;
btn.frame = CGRectMake( btnX, btnY, btnW, 30);
}
}
-(void)test3{
for (NSInteger i = 0 ; i < 100; i++) {
CGFloat btnW = (ScrWidth - 60 ) / 3.0;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:[NSString stringWithFormat:@"%ld", i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.layer.borderWidth = 1.0;
btn.layer.cornerRadius = 5.0;
btn.backgroundColor = [UIColor whiteColor];
[self.scrollView addSubview:btn];
CGFloat btnX = i / 5 * (btnW +20) + 10;
CGFloat btnY = i % 5 * 35;
btn.frame = CGRectMake( btnX, btnY, btnW, 30);
}
}
- (UIScrollView *)scrollView{
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] init];
_scrollView.backgroundColor = [UIColor grayColor];
_scrollView.frame = CGRectMake(0, 50, ScrWidth, 200);
}
return _scrollView;
}
@end