2011-10-19 64 views
0

我已实施UITapGestureRecognizerUIImageView,它正在第一次点击。在第一次点击时,我隐藏了该图像并开始动画。一旦动画完成后,我再次显示图像。但在设置setHidden:FALSE之后,我没有收到那个UIImageView的Tap事件。UIImageView无法识别第二次手势

以下是我使用的代码:

- (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; 

} 

回答

0

确保

时的UIImageView是隐藏的。它不接收任何触摸事件

组阿尔法为零的UIImageView

+0

是的,你说得对,但这里的问题不是因为隐藏或显示图像。即使我不隐藏/显示,但我无法第二次获得图像的轻击手势,但是第一次点击时,一切都可以正常工作。我不知道发生了什么,我无法找出背后的原因。 –

1

viewhidden或其alpha组分是zero该查看将不会收到任何UIGestureRecognizers

我可以建议,如果你需要隐藏一些人认为用另一个方法(我们将其命名为touchableView),但希望它响应手势:

  1. 用相同的帧touchableView创建backgroundView

    backgroundViewclearColor

    UIView *backgroundView = [[UIView alloc] initWithFrame:touchableView.frame];

  2. 设置背景色:

    backgroundView.backgroundColor = [UIColor clearColor];

  3. touchableView复位位置:的touchableView

    CGRect frame = touchableView.frame; frame.origin.x = 0; frame.origin.y = 0;

  4. 禁用用户交互:

    touchableView.userInteractionEnabled = NO;

  5. 作为子视图添加到touchableViewbackgroundView

    [backgroundView addSubview:touchableView];

  6. 添加适当的手势识别来backgroundView

  7. 添加backgroundView以查看您想要的。

现在你可以隐藏touchableView但你仍然会收到手势识别器。

我不测试这个,但我认为它应该工作。

+0

是的,你说得对,但这里的问题不是因为隐藏或显示图像。即使我不隐藏/显示,但我无法第二次获得图像的轻击手势,但是第一次点击时,一切都可以正常工作。我不知道发生了什么,我无法找出背后的原因。 –

+0

你说过:'但是在设置了setHidden:FALSE之后,我没有得到那个UIImageView的Tap事件。如果你不隐藏图像,你不能说它工作错了吗?我花了10分钟写这篇文章=( – Nekto

+0

对不起,但发布后,我试着没有隐藏图像的代码,那时我知道这不是因为隐藏图像,它看起来像是一些其他问题。 –