UITapGesture不工作?

时间:2016-10-26 07:44:24

标签: ios objective-c iphone uitapgesturerecognizer

代码:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OpenBrandUrlLinkMetod:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
FirstImgV.userInteractionEnabled = YES;
[FirstImgV addGestureRecognizer:singleTap];

-(void)OpenBrandUrlLinkMetod:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"hello!!"); 
}

但是没有调用OpenBrandUrlLinkMetod。

任何人都可以帮助我。

4 个答案:

答案 0 :(得分:0)

试试这个,

UITapGestureRecognizer

设置委托
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OpenBrandUrlLinkMetod:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
singleTap.delegate = self;  // set delegate to self
_img.userInteractionEnabled = YES;
[_img addGestureRecognizer:singleTap];

通过以下代码检查手势识别器是否正在调用UIImageview

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([[touch.view class] isEqual:[UIImageView class]]) {
        return YES;
    }else{
        return NO;
    }
}

然后使用TapGesture方法完成你的工作,

-(void)OpenBrandUrlLinkMetod:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"hello!!");
}

答案 1 :(得分:0)

在任何触摸事件发生之前,

singleTap可能会被垃圾收集 为了防止这种情况,你必须将它保留在你班级的某个地方:

@interface YourClass

/// Your gesture recognizer gets referenced/retained here:
@property (strong, nonatomic) UITapGestureRecognizer *singleTap;

@end

然后:(可能在viewDidLoad中)

_singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(OpenBrandUrlLinkMetod:)];
_singleTap.numberOfTapsRequired = 1;
_singleTap.numberOfTouchesRequired = 1;
FirstImgV.userInteractionEnabled = YES;
[FirstImgV addGestureRecognizer: _singleTap];

答案 2 :(得分:0)

我将您的代码完全放入我的项目中,您可以在此VIDEO中看到代码和viewcontroller。

我只需添加此委托iframe

只需添加此委托即可尝试

答案 3 :(得分:0)

我认为你在相关的视图控制器中有一些其他的手势识别器。在这种情况下,这段代码应该让你去。或者尝试使用此代码,并设置代理。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}