- (void)viewDidLoad {
[super viewDidLoad];
//每个Item宽高
CGFloat W = 80;
CGFloat H = 100;
//每行列数
NSInteger rank = 3;
//每列间距
CGFloat rankMargin = (self.view.frame.size.width - rank*W)/(rank+1);
//每行间距
CGFloat rowMargin = 20;
//Item索引 -->根据需求改变索引
NSUInteger index = 9;
for (int i = 0; i<index ; i++) {
//Item X 轴
CGFloat X = (i%rank)*(W+rankMargin)+rankMargin;
//Item Y轴
NSUInteger Y = (i/rank)*(H+rowMargin);
//Item top
CGFloat top = 50;
UIView *speedView = [[UIView alloc]init];
speedView.tag = i;
speedView.frame = CGRectMake(X, Y+top, W, H);
UIImage *image = [UIImage imageNamed:@"IMG_0449"];
speedView.layer.contents = (id)image.CGImage;
[self.view addSubview:speedView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
tap.view.tag = speedView.tag;
[speedView addGestureRecognizer:tap];
}
}
-(void)tap:(UITapGestureRecognizer*)GestureRecognizer{
NSLog(@"======%ld",GestureRecognizer.view.tag);//lable的tag
}