clickx 作為一個方法被調用,表示當你點擊圖片的時候出發這個方法,這個方法可以自己隨便寫比如abc,clicky
代碼如下 復制代碼- (IBAction)test:(id)sender {
UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
imageView.image=[UIImage imageNamed:@"22.png"];
imageView.userInteractionEnabled = YES;
[imageView setTag:2];//為了帶個參數過去
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickx:)];
[imageView addGestureRecognizer:singleTap];
//[singleTap release];
[self.view addSubview:imageView];
}
UITapGestureRecognizer 帶過來的參數 gesture 將索引值tag給傳遞過來
代碼如下 復制代碼- (IBAction)clickx:(id)sender,UITapGestureRecognizer *gesture{
UIView *temView = [gesture view];
NSInteger temIndex = temView.tag;
NSLog(@"aaaa%d",temIndex);
}