2012-07-09 71 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,将其拖出一点以形成背景,然后您可以对其进行设置,以便在收到触摸事件时,在背景图像持续时间内显示不同的自定义颜色。

希望有帮助!