给控件添加点击响应事件UITapGestureRecognizer

来源:互联网 发布:中标麒麟软件大全 编辑:程序博客网 时间:2024/05/05 04:27

在Iphone开发中,像UIimageView是不支持点击的,但往往我们却有很多能在Image上点击的需求,比如一个自定义的TableViewCell中放入三个UIimageView,在这里命名为imageleft,imagemiddle,imggeright,当tableView加载后,单击tableView中某一行中的image,我便进入该图片的详细页面。

当然,现在的最新版支持手势控件,只要拖一个这样的控件到UIImageView上,实现它的委托就可以了。若版本太低不支持这样的控件,你便只好老老实实的亲手写代码了。

[objc] view plain copy
  1. <span style="color:rgb(64,50,38)">#import <UIKit/UIKit.h>  
  2.   
  3. @protocol TableGridViewCellDelegate;  
  4.   
  5. @interface TableGridViewCell : UITableViewCell {  
  6.   
  7. }  
  8.   
  9. @property (nonatomic,retain) IBOutlet UIImageView *imageleft;  
  10. @property (nonatomic,retain) IBOutlet UIImageView *imagemiddle;  
  11. @property (nonatomic,retain) IBOutlet UIImageView *imageright;  
  12. @property (nonatomic,assign) id<TableGridViewCellDelegate> delegate;  
  13. ...  
  14. - (void) configGesture;  
  15. - (void) handTap:(UITapGestureRecognizer*) gesture;<br style="margin:0px; padding:0px">...  
  16. @end  
  17.   
  18. @protocol TableGridViewCellDelegate <NSObject>  
  19. - (void) tapedImageViewInCell:(UITableViewCell*)cell withIndex:(int)index;  
  20. @end  
  21.   
  22. //m文件  
  23. #import "TableGridViewCell.h"  
  24.   
  25. @implementation TableGridViewCell  
  26.   
  27. @synthesize imageleft,imagemiddle,imageright,delegate;  
  28.   
  29. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  30. {  
  31.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  32.     if (self) {  
  33.         // Initialization code  
  34.     }  
  35.     return self;  
  36. }  
  37.   
  38. - (void)setSelected:(BOOL)selected animated:(BOOL)animated  
  39. {  
  40.     [super setSelected:selected animated:animated];  
  41.   
  42.     // Configure the view for the selected state  
  43. }  
  44.   
  45. -(void) dealloc{  
  46.     SAFE_RELEASE(imageleft);  
  47.     SAFE_RELEASE(imagemiddle);  
  48.     SAFE_RELEASE(imageright);  
  49.     [super dealloc];  
  50. }  
  51.   
  52. </span><span style="color:#3366ff">- (void) configGesture  
  53. {  
  54.     UITapGestureRecognizer *_left = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handTap:)];  
  55.     [imageleft addGestureRecognizer:_left];  
  56.     SAFE_RELEASE(_left);  
  57.       
  58.     UITapGestureRecognizer *_mid = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handTap:)];  
  59.     [imagemiddle addGestureRecognizer:_mid];  
  60.     SAFE_RELEASE(_mid);  
  61.       
  62.     UITapGestureRecognizer *_right = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handTap:)];  
  63.     [imageright addGestureRecognizer:_right];  
  64.     SAFE_RELEASE(_right);  
  65. }  
  66.   
  67. - (void) handTap:(UITapGestureRecognizer*) gesture  
  68. {  
  69.     if ([delegate respondsToSelector:@selector(tapedImageViewInCell:withIndex:)]) {  
  70.         UIImageView *v = (UIImageView*)gesture.view;  
  71.         [delegate tapedImageViewInCell:self withIndex:v.tag];  
  72.     }  
  73. }</span><span style="color:#403226">  
  74. <br style="margin:0px; padding:0px">#pragma mark -<br style="margin:0px; padding:0px">#pragma mark TableGridViewCellDelegate<br style="margin:0px; padding:0px">- (void) tapedImageViewInCell:(UITableViewCell*)cell withIndex:(int)index<br style="margin:0px; padding:0px">{<br style="margin:0px; padding:0px">    int row = [myTable indexPathForCell:cell].row;<br style="margin:0px; padding:0px">    VODItem *it = (VODItem*)[ipListarry objectAtIndex:row*3+index];<br style="margin:0px; padding:0px">    NSString *preview_Url = [[NSString alloc] initWithFormat:@"%@",it.Preview_Url];<br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px">    IPCellDetailInfo *IPCellDetailInfoController = [[IPCellDetailInfo alloc]init];<br style="margin:0px; padding:0px">    IPCellDetailInfoController.ClipDetialsInterfaceUrl=it.ClipDetialsInterfaceUrl;<br style="margin:0px; padding:0px">    IPCellDetailInfoController.ClipDetialsTitle = it.Primary_Name;<br style="margin:0px; padding:0px">    IPCellDetailInfoController.btnPlayOrViewTitle = it.Sndlvl_Desc;<br style="margin:0px; padding:0px">  <br style="margin:0px; padding:0px">    [self.navigationController pushViewController:IPCellDetailInfoController animated:YES]; //新视图压入到栈中<br style="margin:0px; padding:0px">    [IPCellDetailInfoController release];<br style="margin:0px; padding:0px">    <br style="margin:0px; padding:0px">    NSLog(@"row = [%d], col = [%d], Preview_Url = [%@]", row, index,preview_Url);<br style="margin:0px; padding:0px">    [preview_Url release];<br style="margin:0px; padding:0px">}  
  75. @end  
  76. </span>  

好了  其实主要就是要会使用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];

}


0 0
原创粉丝点击