九宫格思想

九宫格思想

在九宫格(不仅限于九宫格)中展示照片之类

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

@property (nonatomic ,strong) UIView *shopView;


@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.shopView = [[UIView alloc] initWithFrame:CGRectMake(10, 20, self.view.bounds.size.width - 20, 300)];
    self.shopView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.shopView];
    
    UIButton *addGoods = [[UIButton alloc] initWithFrame:CGRectMake(10, 350, 50, 20)];
    addGoods.backgroundColor = [UIColor blueColor];
    [self.view addSubview:addGoods];
        [addGoods addTarget:self action:@selector(addGoodsClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)addGoodsClick {

    NSInteger line = 4;
    CGFloat width = 60;
    CGFloat height = 100;
    CGFloat wMargin = (self.shopView.frame.size.width - line * width)/(line - 1);
    CGFloat hMargin = 10;
    NSInteger index = self.shopView.subviews.count;
    CGFloat x = (index % line) * (width + wMargin);
    CGFloat y = (index / line) * (height + hMargin);
    
    UIView *goodsView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    goodsView.backgroundColor = [UIColor blackColor];
    [self.shopView addSubview:goodsView];
    
}
原文地址:https://www.cnblogs.com/igeniuswwh/p/6209173.html