将 UIGesture 添加到按钮

发布于 2024-10-16 21:04:44 字数 110 浏览 5 评论 0原文

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

I created a number of buttons in IB and linked them to actions successfully. But I would like to have another action occur if the button is pinched. There is probably something basic that I have missed but I don't seem to be able to figure out how to add the UIPinchGestureRecognizer to the button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷夜 2024-10-23 21:04:44

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

i dont u can add a UIPinchGestureRecognizer to the button normally we will add it into the View right. i think for UIButton the actions available just like "TouchUpInside" as defined in the IB.

ゝ偶尔ゞ 2024-10-23 21:04:44

据我所知,您可以向 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
}

As far as I know, you can add a gesture to a UIButton.

Here is a small piece of code to give you a general idea:

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(someAction:)];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addGestureRecognizer:pinch];

I don't think you have to do the whole initWithTarget:self action:... but this is the only way I have successfully done visual changes that are caused by UIGestures of some type.

If your following my example: @selector(someAction:)]; you will declare the method like this:

-(void)someAction:(UIPinchGestureRecognizer *)sender
{
     //do what ever changes you want the gesture to cause here.
     //e.g. change the button size, color, shape
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文