程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> cocoa touch-網格視圖圖片的按鈕問題

cocoa touch-網格視圖圖片的按鈕問題

編輯:編程綜合問答
網格視圖圖片的按鈕問題

有一個網格視圖的圖片,怎麼為每個圖片添加一個點擊事件?

- (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];


}

最佳回答:


可以用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
        }
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved