代码:
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。
任何人都可以帮助我。
答案 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)
答案 3 :(得分:0)
我认为你在相关的视图控制器中有一些其他的手势识别器。在这种情况下,这段代码应该让你去。或者尝试使用此代码,并设置代理。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}