在TableViewCell ContentView上使用TapGesture

时间:2013-08-13 06:08:07

标签: iphone ios6 xcode4.6 uitapgesturerecognizer

我有一个自定义的tableviewcell子类。

tableview单元格包含UIImgaveView,UIButton和其他Controlls。  我在tableViewcell ContentView中添加了这些。

我必须在UIImageView上应用UITapGestureRecognizer

如何将tapGesture应用于imageview?

2 个答案:

答案 0 :(得分:0)

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;

答案 1 :(得分:0)

将以下代码放在您想要调用它的方法中..: -

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;

    }
    }

享受编码..