Scrollview放多个按钮的2种排列3种计算方式

test01和test02运行效果


Scrollview放多个按钮的2种排列3种计算方式_第1张图片
01.png

test03运行效果


Scrollview放多个按钮的2种排列3种计算方式_第2张图片
02.png
#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

你可能感兴趣的:(Scrollview放多个按钮的2种排列3种计算方式)