1. 程式人生 > >給控制元件新增單擊事件--UITapGestureRecognizer

給控制元件新增單擊事件--UITapGestureRecognizer

在Iphone開發中,像UIimageView是不支援點選的,但往往我們卻有很多能在Image上點選的需求,比如一個自定義的TableViewCell中放入三個UIimageView,在這裡命名為imageleft,imagemiddle,imggeright,當tableView載入後,單擊tableView中某一行中的image,我便進入該圖片的詳細頁面。

當然,現在的最新版支援手勢控制元件,只要拖一個這樣的控制元件到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(長按)。


比如一個很簡單的開關實現,使用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:[UIImageimageNamed:@"on.png"]];

    [imgView setHighlightedImage:[UIImageimageNamed:@"off.png"]];

UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleTap:)];

    [imgView addGestureRecognizer:tap];

    [imgView setUserInteractionEnabled:YES];

    [self.view addSubview:imgView];

}