2011-10-19 66 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

對不起,但發佈後,我試着沒有隱藏圖像的代碼,那時我知道這不是因爲隱藏圖像,它看起來像是一些其他問題。 –