UIImageView 无法识别第二次手势
我已经在 UIImageView
上实现了 UITapGestureRecognizer
,它在第一次点击时就可以工作。在第一次点击时,我隐藏该图像并开始动画。动画完成后,我将再次显示图像。但是设置 setHidden:FALSE
后,我没有收到该 UIImageView
的 Tap 事件。
以下是我正在使用的代码:
- (void)viewDidLoad{
[super viewDidLoad];
defaultDogView= [[UIImageView alloc] initWithFrame:CGRectMake(3, 270, 110, 210)];
[defaultDogView setImage:[UIImage imageNamed:@"dog1.png"]];
defaultDogView.userInteractionEnabled = YES;
[self addGestureRecognizersToPiece:defaultDogView];
[self.view addSubview:defaultDogView];
}
- (void)addGestureRecognizersToPiece:(UIImageView *)piece
{
NSLog(@"in Gesture");
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapPiece:)];
[tapGesture setDelegate:self];
[piece addGestureRecognizer:tapGesture];
[tapGesture release];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressPiece:)];
[piece addGestureRecognizer:longPressGesture];
[longPressGesture release];
NSLog(@"%@", [piece gestureRecognizers]);
}
- (void)singleTapPiece:(UITapGestureRecognizer *)gestureRecognizer
{
NSLog(@"Image Tapped");
/** Hide the default Image and start the animation ***/
[defaultDogView setHidden:TRUE];
/***Animating the Dog***/
[dogArray addObject:[SpriteHelpers setupAnimatedDog:self.view numFrames:69 withFilePrefix:@"dog" withDuration:(12) ofType:@"png" withValue:0]];
dogView = [dogArray objectAtIndex:0];
//[self addGestureRecognizersToPiece:dogView];
[self performSelector:@selector(callBubbleUpdater) withObject:nil afterDelay:5.5];
}
-(void)showDogFrame{
NSLog(@"%@",[defaultDogView gestureRecognizers]);
[defaultDogView setHidden:FALSE];
defaultDogView.userInteractionEnabled = YES;
}
I have implemented UITapGestureRecognizer
on UIImageView
, It is working on first tap. On First Tap, I am hiding that image and starting animation. Once the animations are completed, i am showing the image again. But After setting setHidden:FALSE
, I am not getting the Tap event of that UIImageView
.
Following is the code I am using :
- (void)viewDidLoad{
[super viewDidLoad];
defaultDogView= [[UIImageView alloc] initWithFrame:CGRectMake(3, 270, 110, 210)];
[defaultDogView setImage:[UIImage imageNamed:@"dog1.png"]];
defaultDogView.userInteractionEnabled = YES;
[self addGestureRecognizersToPiece:defaultDogView];
[self.view addSubview:defaultDogView];
}
- (void)addGestureRecognizersToPiece:(UIImageView *)piece
{
NSLog(@"in Gesture");
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapPiece:)];
[tapGesture setDelegate:self];
[piece addGestureRecognizer:tapGesture];
[tapGesture release];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressPiece:)];
[piece addGestureRecognizer:longPressGesture];
[longPressGesture release];
NSLog(@"%@", [piece gestureRecognizers]);
}
- (void)singleTapPiece:(UITapGestureRecognizer *)gestureRecognizer
{
NSLog(@"Image Tapped");
/** Hide the default Image and start the animation ***/
[defaultDogView setHidden:TRUE];
/***Animating the Dog***/
[dogArray addObject:[SpriteHelpers setupAnimatedDog:self.view numFrames:69 withFilePrefix:@"dog" withDuration:(12) ofType:@"png" withValue:0]];
dogView = [dogArray objectAtIndex:0];
//[self addGestureRecognizersToPiece:dogView];
[self performSelector:@selector(callBubbleUpdater) withObject:nil afterDelay:5.5];
}
-(void)showDogFrame{
NSLog(@"%@",[defaultDogView gestureRecognizers]);
[defaultDogView setHidden:FALSE];
defaultDogView.userInteractionEnabled = YES;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当
view
处于隐藏
或其alpha
组件零
时,该视图将不会接收任何UIGestureRecognizers< /代码>。
如果您需要隐藏某些视图(让我们将其命名为
touchableView
)但希望它响应手势,我建议使用下一种方法:使用相同的方法创建
backgroundView
框架为touchableView
:UIView *backgroundView = [[UIView alloc] initWithFrame:touchableView.frame];
设置
backgroundView
的背景颜色为clearColor
:backgroundView.backgroundColor = [UIColor clearColor];
重置
touchableView
的位置:CGRect框架=touchableView.frame;
框架.origin.x = 0;
frame.origin.y = 0;
禁用
touchableView
的用户交互:touchableView.userInteractionEnabled = NO;
添加
touchableView
作为backgroundView
的子视图:[backgroundView addSubview:touchableView];
向
backgroundView
添加适当的手势识别器。添加
backgroundView
以查看您想要的视图。现在您可以隐藏
touchableView
但您仍然会收到手势识别器。我没有测试这个,但我认为它应该有效。
When
view
ishidden
or itsalpha
component iszero
that view won't receive anyUIGestureRecognizers
.I can suggest to use next approach if you need to hide some view (let's name it
touchableView
) but want it to respond to gestures:Create
backgroundView
with the same frame astouchableView
:UIView *backgroundView = [[UIView alloc] initWithFrame:touchableView.frame];
Set background color of
backgroundView
toclearColor
:backgroundView.backgroundColor = [UIColor clearColor];
Reset position of
touchableView
:CGRect frame = touchableView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
Disable user interaction of
touchableView
:touchableView.userInteractionEnabled = NO;
Add
touchableView
as subview tobackgroundView
:[backgroundView addSubview:touchableView];
Add appropriate gesture recognizers to
backgroundView
.Add
backgroundView
to view that you want.Now you can hide
touchableView
but you will still receive gesture recognizers.I don't test this but I think it should work.
确定
UIImageView 何时隐藏。它不接收任何触摸事件,
为 uiimageview 设置 alpha 零
sure
when UIImageView is hidden. it does not receive any touch events
set alpha zero for uiimageview