2012-07-09 86 views
0

問題:

我試圖改變視圖的背景顏色使用UITapGestureRecognizer。本質上,當用戶點擊時,我希望背景改變顏色,在發佈或完成時,背景顏色會回到零,或者沒有背景顏色。iOS - UITapGesture開始和結束了嗎?

此操作與默認的UIButton類似,但我需要將它放在視圖上。

問題:

- (void)handleTapGesture:(UITapGestureRecognizer *)recognizer 
{ 
     if(recognizer.state == UIGestureRecognizerStateBegan) { 
      self.backgroundColor = [UIColor greenColor]; 
     } 

     if(recognizer.state == UIGestureRecognizerStateEnded) { 
      self.backgroundColor = nil; 
     } 
} 

沒有任何sucess:

我已經使用類似的代碼來嘗試。這是知道點按手勢何時開始和結束的正確方法嗎?

+0

你如何將你的姿態添加到你的視圖?阿魯你確定 - handleTapGesture被正確調用? – Pierre 2012-07-09 15:55:09

+0

您正在爲「自我」設置backgroundColor。自己是(UIView的子類)嗎? – 2012-07-09 16:33:36

+0

@Scott是的,self是UIView的子類。 – Romes 2012-07-09 17:01:40

回答

1

這可能是更容易使用這些方法,它應當已經在UIViewController響應鏈的一部分,除非他們正在其他地方或通過界面生成器處理已經

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    //CGPoint touchPoint = [[touches anyObject] locationInView:self.view]; 
    NSLog(@"began"); 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    //CGPoint touchPoint = [[touches anyObject] locationInView:self.view]; 
    NSLog(@"end"); 
} 
+1

謝謝,這有幫助! – Romes 2012-07-09 17:30:05

0

如果您使用的故事板不知爲何,你可以使用UIButton,將其拖出一點以形成背景,然後您可以對其進行設置,以便在收到觸摸事件時,在背景圖像持續時間內顯示不同的自定義顏色。

希望有幫助!