bluetooth99 2012-12-24 05:20 采纳率: 0%
浏览 1960
已采纳

网格视图图片的按钮问题

有一个网格视图的图片,怎么为每个图片添加一个点击事件?

- (void)viewDidLoad
{
[super viewDidLoad];


int Columns = 3;
int Rows=5;

int space = 10;
int width = (self.view.frame.size.width-(Columns)*space)/Columns;
int height = width;
int x = space;
int y = space;  
UIScrollView *Scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y,self.view.frame.size.width, self.view.frame.size.height) ];
[Scroll setContentSize:CGSizeMake(Columns*(space+width)+space, Rows*(space+(self.view.frame.size.height-(Columns)*space)/Columns)+space)];
Scroll.backgroundColor=[UIColor yellowColor];
Scroll.showsVerticalScrollIndicator=YES;

for(int i=1 ;i<30;i++)
{
    j++;


    NSLog(@"j= %i",j);


    label=[[UILabel alloc]initWithFrame:CGRectMake(x,y,width,height)];
    label.backgroundColor=[UIColor blueColor];

    image= [[UIImageView alloc] initWithFrame:CGRectMake(x,y,width,height)];
    image.image = [UIImage imageNamed:
                   [NSString stringWithFormat:@"image%02ds.jpg", i+1]]; 



    [Scroll addSubview:label];

    if (i%Columns == 0) {
        y += space+height;
        x = space;
    } else {
        x+=space+width;
    }
    [Scroll addSubview:image];

}
[self.view addSubview:Scroll];


}
  • 写回答

1条回答 默认 最新

  • g989_1314125 2012-12-24 06:03
    关注

    可以用UITapGestureRecognizer实现

    UITapGestureRecognizer *tapRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
    imageView.tag=TAG; //Tag your imageview to identify in call back
    [imageView addGestureRecognizer:tapRecognizer];
    [tapRecognizer release];    //If not ARC
    

    回调函数如下.

    -(void)imageTapped:(UITapGestureRecognizer *)tapRecognizer
    {
        if ([tapRecognizer.view isKindOfClass:[UIImageView class]]) {
            if (tapRecognizer.view.tag==TAG) { //Identify image view tag
                //Your code for image tap action
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试