我有一个定制的 tableviewcell 子类。
tableview 单元格包含 UIImgaveView 、 UIButton 和其他控件。我在 tableViewcell ContentView 中添加了这些。
我必须申请 UITapGestureRecognizer
UIImageView 。
如何将 tapGesture 应用于 imageview?
我有一个定制的 tableviewcell 子类。
tableview 单元格包含 UIImgaveView 、 UIButton 和其他控件。我在 tableViewcell ContentView 中添加了这些。
我必须申请 UITapGestureRecognizer
UIImageView 。
如何将 tapGesture 应用于 imageview?
将以下代码放入您要调用它的方法中..:-
UITapGestureRecognizer *tapOnView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[tapOnView setNumberOfTouchesRequired:1];//Number of touches Depends on your requirement
[tapOnView setNumberOfTapsRequired:1];//Number of taps Depends on your requirement
[tapOnView setDelegate:self];//delegate
contentView.userInteractionEnabled = YES;//Make view userInteractionEnabled,content view is yours image view
[cell.contentView addGestureRecognizer:tapOnView];//Adding TapGesture to view.
- (IBAction)tap:(id)sender
{
NSLog(@"your requirement");//you can perform action here.
if ([sender isKindOfClass:[UIGestureRecognizer self]]) {
// it's a gesture recognizer. we can cast it and use it like this
UITapGestureRecognizer *tapGR = (UITapGestureRecognizer *)sender;
} else if ([sender isKindOfClass:[UIButton self]]) {
// it's a button
UIButton *button = (UIButton *)sender;
}
}
享受编码..
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap)];
[tapGR setNumberOfTouchesRequired:1];
[tapGR setNumberOfTapsRequired:1];
[tapGR setDelegate:self];
[cell.yourImageView addGestureRecognizer:tapGR]; // Your ImageView of contentView
[tapGR release]; //If you aren't using ARC
- (void)onTap
{
NSLog(@"Image tapped");
}
注意:-确保包含<UIGestureRecognizerDelegate>
在头文件中,并且应该为UIImageView
.yourImageView.userInteractionEnabled = YES;