道理差不多,简单写写,就是给UIImage所在的UIImageView添加个单击的手势,让用户点击图片时有响应的响应。 有人用个透明的UIButton,感觉有时候不方便。
- (void)viewDidLoad
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(whenClickImage)];
[imageView addGestureRecognizer:singleTap];
[imageView setImage:[UIImage imageNamed:@"1.png"]];
[singleTap release];
[self.view addSubview: imageView];
}
-(void)whenClickImage
{
NSLog(@"you click image");
}
不仅UIImageView可以添加,UIScrollView上也可以。还可以添加其他双击旋转等手势。 比较简单实用。