for (int i = 0; i < totalCount; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
// 图片X
CGFloat imageX = i * imageW;
// 设置frame
imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
// 设置图片
NSString *name = [NSString stringWithFormat:@"公交宣传语%d.jpg", i + 1];
imageView.image = [UIImage imageNamed:name];
imageView.tag = i;
imageView.userInteractionEnabled=YES;
UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickImage:)];
[imageView addGestureRecognizer:singleTap];
//imageView.contentMode = UIViewContentModeScaleAspectFit;
// 隐藏指示条
self.scrollview.showsHorizontalScrollIndicator = NO;
[self.scrollview addSubview:imageView];
}
-(void)onClickImage:(UITapGestureRecognizer *)recognizer
{
UIImageView* imageView = (UIImageView*)[recognizer view];
for (int i=0; i<4; i++) {
if(imageView.tag == i)
NSLog(@"图片%d被点击!",i);
}
}
网友评论