【问题标题】:Adding a UIGesture to a button将 UIGesture 添加到按钮
【发布时间】:2011-02-10 03:55:49
【问题描述】:

我在 IB 中创建了许多按钮并将它们成功链接到操作。但是如果按钮被捏住,我希望发生另一个动作。我可能错过了一些基本的东西,但我似乎无法弄清楚如何将 UIPinchGestureRecognizer 添加到按钮。

【问题讨论】:

    标签: ios uibutton uigesturerecognizer


    【解决方案1】:

    我不能将 UIPinchGestureRecognizer 添加到按钮,通常我们会将其添加到视图右侧。我认为对于 UIButton,可用的操作就像 IB 中定义的“TouchUpInside”。

    【讨论】:

      【解决方案2】:

      据我所知,您可以向UIButton 添加手势。

      这里有一小段代码可以让你大致了解一下:

      UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(someAction:)];
      
      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [button addGestureRecognizer:pinch];
      

      我认为您不必完成整个initWithTarget:self action:...,但这是我成功完成由某种类型的UIGestures 引起的视觉变化的唯一方法。

      如果您遵循我的示例:@selector(someAction:)];,您将声明这样的方法:

      -(void)someAction:(UIPinchGestureRecognizer *)sender
      {
           //do what ever changes you want the gesture to cause here.
           //e.g. change the button size, color, shape
      }
      

      【讨论】:

        猜你喜欢
        • 2017-04-09
        • 2017-08-15
        • 2018-06-13
        • 2013-03-07
        • 2014-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多