在Iphone开发中,像UIimageView是不支持点击的,但往往我们却有很多能在Image上点击的需求,比如一个自定义的TableViewCell中放入一个UIimageView,在这里命名为imageleft,当tableView加载后,单击tableView中某一行中的image,我便进入该图片的详细页面。
当然,现在的最新版支持手势控件,只要拖一个这样的控件到UIImageView上,实现它的委托就可以了。若版本太低不支持这样的控件,你便只好老老实实的亲手写代码了。
好了 其实主要就是要会使用UITapGestureRecognizer,当然这只是手势的其中一个。下面还有几个如:
- UITapGestureRecognizer
- UIPinchGestureRecognizer
- UIRotationGestureRecognizer
- UISwipeGestureRecognizer
- UIPanGestureRecognizer
- UILongPressGestureRecognizer
从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。
比如一个很简单的开关实现,使用UIImageView的手势来实现,这种情况一般加在TableViewCell里面很好用
-(void)handleTap:(id)sender{
UITapGestureRecognizer *tap = sender;
UIImageView *imgView = (UIImageView*)tap.view;
[imgView setHighlighted:!imgView.highlighted];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
[imgView setImage:[UIImage imageNamed:@"on.png"]];
[imgView setHighlightedImage:[UIImage imageNamed:@"off.png"]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
[imgView addGestureRecognizer:tap];
[imgView setUserInteractionEnabled:YES];
[self.view addSubview:imgView];
}