xcode 点击手势不适用于 UIimageview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10118785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tap gesture is not working on UIimageview
提问by Priya
In my application i have three uiimageview which is moving randomly. on single tap on imageview it should hide. But my tapgesture is not working. on single tap it is not getting hide.
在我的应用程序中,我有三个随机移动的 uiimageview。在 imageview 上单击它应该隐藏。但是我的tapgesture 不起作用。单击一下,它不会被隐藏。
- (void)showAlert1:(UITapGestureRecognizer *)sender
{
if (image1.tag == 1)
{
image1.hidden = TRUE;
}
else
{
image1.hidden = FALSE;
}
}
- (void)showAlert2:(UITapGestureRecognizer *)sender
{
if (image1.hidden == TRUE && image3.hidden == FALSE)
{
image2.hidden = TRUE;
}
else
{
image2.hidden = FALSE;
}
}
- (void)showAlert3:(UITapGestureRecognizer *)sender
{
if (image1.hidden == TRUE && image2.hidden == TRUE)
{
image3.hidden = TRUE;
}
else
{
image3.hidden = FALSE;
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (image1.tag == 1)
{
image1.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(imageAlerts];
tap.numberOfTapsRequired = 1;
[image1 addGestureRecognizer:tap];
}
if (image2.tag == 2)
{
image2.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert2];
tap.numberOfTapsRequired = 1;
[image2 addGestureRecognizer:tap];
}
if (image3.tag == 3)
{
image3.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert3];
tap.numberOfTapsRequired = 1;
[image3 addGestureRecognizer:tap];
}
}
Can anyone help me?
谁能帮我?
Thanks in advance
提前致谢
采纳答案by hp iOS Coder
Try this
尝试这个
- (void)showAlert1:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
// your handling code
if (image1.tag==1)
image1.hidden=TRUE;
else
image1.hidden=FALSE;
}
}
回答by Charan
Please checkmark the userInteractionEnabled
and multipleTouch
in xib file if you have added image in xib
如果您在 xib 中添加了图像,请勾选xib 文件中的userInteractionEnabled
和multipleTouch
or
或者
image.userInteractionEnabled = YES;
image.multipleTouchEnabled = YES;
in ViewDidLoad
在 ViewDidLoad
回答by Josef Rysanek
Did you implement UIGestureRecognizerDelegate and set in to self?
您是否实现了 UIGestureRecognizerDelegate 并设置为 self?
回答by iOSPawan
try this -
尝试这个 -
- (void)viewWillAppearBOOL:animated
{
[super viewWillAppear:animated];
if (image1.tag==1)
{
image1.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert1:];
tap.numberOfTapsRequired = 1;
[image1 addGestureRecognizer:tap];
}
if (image2.tag==2)
{
image2.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert2:];
tap.numberOfTapsRequired = 1;
[image2 addGestureRecognizer:tap];
}
if (image3.tag==3)
{
image3.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert3:];
tap.numberOfTapsRequired = 1;
[image3 addGestureRecognizer:tap];
}
}
Also it has memory leak. UIGesture
is not released after it adds to image.
它也有内存泄漏。UIGesture
添加到图像后不会释放。