给控件添加单击事件--UITapGestureRecognizer
2012-08-14 16:51
423 查看
在Iphone开发中,像UIimageView是不支持点击的,但往往我们却有很多能在Image上点击的需求,比如一个自定义的TableViewCell中放入三个UIimageView,在这里命名为imageleft,imagemiddle,imggeright,当tableView加载后,单击tableView中某一行中的image,我便进入该图片的详细页面。
当然,现在的最新版支持手势控件,只要拖一个这样的控件到UIImageView上,实现它的委托就可以了。若版本太低不支持这样的控件,你便只好老老实实的亲手写代码了。
好了 其实主要就是要会使用UITapGestureRecognizer,当然这只是手势的其中一个。下面还有几个如:
UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecognizer
UISwipeGestureRecognizer
UIPanGestureRecognizer
UILongPressGestureRecognizer
从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。
当然,现在的最新版支持手势控件,只要拖一个这样的控件到UIImageView上,实现它的委托就可以了。若版本太低不支持这样的控件,你便只好老老实实的亲手写代码了。
#import <UIKit/UIKit.h> @protocol TableGridViewCellDelegate; @interface TableGridViewCell : UITableViewCell { } @property (nonatomic,retain) IBOutlet UIImageView *imageleft; @property (nonatomic,retain) IBOutlet UIImageView *imagemiddle; @property (nonatomic,retain) IBOutlet UIImageView *imageright; @property (nonatomic,assign) id<TableGridViewCellDelegate> delegate; ... - (void) configGesture; - (void) handTap:(UITapGestureRecognizer*) gesture; ... @end @protocol TableGridViewCellDelegate <NSObject> - (void) tapedImageViewInCell:(UITableViewCell*)cell withIndex:(int)index; @end //m文件 #import "TableGridViewCell.h" @implementation TableGridViewCell @synthesize imageleft,imagemiddle,imageright,delegate; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } -(void) dealloc{ SAFE_RELEASE(imageleft); SAFE_RELEASE(imagemiddle); SAFE_RELEASE(imageright); [super dealloc]; } - (void) configGesture { UITapGestureRecognizer *_left = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handTap:)]; [imageleft addGestureRecognizer:_left]; SAFE_RELEASE(_left); UITapGestureRecognizer *_mid = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handTap:)]; [imagemiddle addGestureRecognizer:_mid]; SAFE_RELEASE(_mid); UITapGestureRecognizer *_right = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handTap:)]; [imageright addGestureRecognizer:_right]; SAFE_RELEASE(_right); } - (void) handTap:(UITapGestureRecognizer*) gesture { if ([delegate respondsToSelector:@selector(tapedImageViewInCell:withIndex:)]) { UIImageView *v = (UIImageView*)gesture.view; [delegate tapedImageViewInCell:self withIndex:v.tag]; } } #pragma mark - #pragma mark TableGridViewCellDelegate - (void) tapedImageViewInCell:(UITableViewCell*)cell withIndex:(int)index { int row = [myTable indexPathForCell:cell].row; VODItem *it = (VODItem*)[ipListarry objectAtIndex:row*3+index]; NSString *preview_Url = [[NSString alloc] initWithFormat:@"%@",it.Preview_Url]; IPCellDetailInfo *IPCellDetailInfoController = [[IPCellDetailInfo alloc]init]; IPCellDetailInfoController.ClipDetialsInterfaceUrl=it.ClipDetialsInterfaceUrl; IPCellDetailInfoController.ClipDetialsTitle = it.Primary_Name; IPCellDetailInfoController.btnPlayOrViewTitle = it.Sndlvl_Desc; [self.navigationController pushViewController:IPCellDetailInfoController animated:YES]; //新视图压入到栈中 [IPCellDetailInfoController release]; NSLog(@"row = [%d], col = [%d], Preview_Url = [%@]", row, index,preview_Url); [preview_Url release]; } @end
好了 其实主要就是要会使用UITapGestureRecognizer,当然这只是手势的其中一个。下面还有几个如:
UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecognizer
UISwipeGestureRecognizer
UIPanGestureRecognizer
UILongPressGestureRecognizer
从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。
相关文章推荐
- 给控件添加单击事件--UITapGestureRecognizer
- 给控件添加单击事件--UITapGestureRecognizer
- 给控件添加单击事件--UITapGestureRecognizer
- 给控件添加单击事件--UITapGestureRecognizer
- iOS-85-UILabel等控件添加手势点击事件(UITapGestureRecognizer)
- 给控件添加点击响应事件UITapGestureRecognizer
- 给UITableView添加UITapGestureRecognizer 每个Item多事件点击出现崩溃
- IOS UITapGestureRecognizer 遇到父view添加UIImageView的背景图片,子subview的tap事件不起作用了
- UIImageview 通过 UITapGestureRecognizer添加点击事件
- ios UITapGestureRecognizer 单指单击、单指多击、多指单击、多指多击事件操作
- ios UITapGestureRecognizer 单指单击、单指多击、多指单击、多指多击事件操作
- 控件事件iOS-手势 UITapGestureRecognizer
- ios UITapGestureRecognizer 单指单击、单指多击、多指单击、多指多击事件操作
- 添加了UITapGestureRecognizer事件的View再添加UITableView导致 didSelectRowAtIndexPath失效
- ios UITapGestureRecognizer 单指单击、单指多击、多指单击、多指多击事件操作
- ios UITapGestureRecognizer 单指单击、单指多击、多指单击、多指多击事件操作
- iOS UIImageView 添加 UITapGestureRecognizer 事件
- 在view上加UITapGestureRecognizer,按钮不触发单击事件的处理方法
- iOS 在添加了UITapGestureRecognizer的UIView上响应按钮的点击事件
- ios UITapGestureRecognizer 单指单击、单指多击、多指单击、多指多击事件操作