一聚教程网:一个值得你收藏的教程网站

热门教程

ios开发之xcode加载图片点击事件,图片带参数

时间:2022-06-25 23:48:55 编辑:袖梨 来源:一聚教程网

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);
}

热门栏目