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 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入