【问题标题】:iOS - UITapGesture did begin and end?iOS - UITapGesture 开始和结束了吗?
【发布时间】:2012-07-09 15:37:08
【问题描述】:

问题:

我正在尝试使用UITapGestureRecognizer 更改视图的背景颜色。本质上,当用户点击时,我希望背景改变颜色,并且在释放或完成时,背景颜色变回零,或者没有背景颜色。

此操作类似于默认的UIButton,但我需要将其显示在视图上。

问题:

我尝试过使用类似的代码:

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

没有任何成功。这是了解轻按手势何时开始和结束的正确方法吗?

【问题讨论】:

  • 如何将手势添加到视图中? Aru 你确定 - 正确调用了 handleTapGesture 吗?
  • 您正在为“self”设置背景颜色。 self 是 UIView 的(子类)吗?
  • @Scott 是的,self 是 UIView 的子类。
  • @Pierre 水龙头在initWithCoder 中处理,我知道它正在被调用。

标签: ios xcode uibutton uitapgesturerecognizer


【解决方案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");
}

【讨论】:

    【解决方案2】:

    如果您使用情节提要或通过界面构建​​器以某种方式使用,您可以使用 UIButton,将其拖出一点以将其形成背景,然后您可以设置它,以便在收到触摸事件时,使用不同的自定义颜色在背景图像被按住期间显示。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多