Objective C 语法:UISwipeGestureRecognizer 上的 addTarget

发布于 2024-12-29 02:50:17 字数 715 浏览 0 评论 0原文

我目前正在开发一个应用程序,用户可以选择滑动浏览数据,或使用按钮浏览数据。我无法理解如何组合两段代码。

这是我用于滑动的代码:(

- (void)swipeRight:(UISwipeGestureRecognizer*)recognizer
{

if ([questions hasPrevQuestion] == YES) {
    [self vorige:nil];

    }    
}

[self vorige:nil]; 正在调用按钮的方法,因此滑动和按钮具有相同的行为) 我需要以某种方式合并适用于按钮的代码:

-(void)animationDidEndOnAnswer {

[vorigeButton addTarget:self action:@selector(newQuestion:) forControlEvents:UIControlEventTouchUpInside];

}

我认为这非常简单,但我只是无法弄清楚如何在这里调用按钮的滑动方法位置...我想它是很简单,因为我在 UIGestureRecognizer 类参考中找到了这个示例:

- (void)addTarget:(id)target action:(SEL)action

但是作为 Objective-C 的新手,我真的不知道该怎么办。非常感谢任何帮助。

I'm currently working on an app where the user has the option to either swipe through data, or use a button to go through the data. I'm having trouble understanding how to combine two bits of code.

Here is the code I'm using for swiping:

- (void)swipeRight:(UISwipeGestureRecognizer*)recognizer
{

if ([questions hasPrevQuestion] == YES) {
    [self vorige:nil];

    }    
}

(the [self vorige:nil]; is calling the method for the button, so the swiping and the button have the same behavior)
and I need to somehow incorporate this code which applies to the button:

-(void)animationDidEndOnAnswer {

[vorigeButton addTarget:self action:@selector(newQuestion:) forControlEvents:UIControlEventTouchUpInside];

}

I think it's pretty simple, but I just cannot for the life of me figure out how to call the swiping method place of the button here...I'm thinking it's simple because I found this example in the UIGestureRecognizer class reference:

- (void)addTarget:(id)target action:(SEL)action

but being new to objective-c, I don't really know what to do with this. any help is very appreciated.

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

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

发布评论

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

评论(2

記柔刀 2025-01-05 02:50:17

- (void)addTarget:(id)target action:(SEL)action 是相当于将按钮操作绑定到方法的代码,就像您从按钮按住 Ctrl 键拖动到 IBAction 时所做的那样界面生成器。

因此,如果您的方法名为 vorige: 并且您希望在点击按钮时调用它,您会说:

[vorigeButton addTarget:self action:@selector(vorige:) forControlEvents:UIControlEventTouchUpInside];

但我不知道您为什么要这样做,因为动画 - 通常在加载视图时设置按钮操作一次,而不是在动画期间更改它。

- (void)addTarget:(id)target action:(SEL)action is the code equivalent of binding a button action to a method like you do when you ctrl-drag from a button to an IBAction in Interface Builder.

So if your method is called vorige: and you want it to be called when the button is tapped, you would say:

[vorigeButton addTarget:self action:@selector(vorige:) forControlEvents:UIControlEventTouchUpInside];

But I don't know why you would want to do that as a result of an animation - you normally would set the button action once when the view is loaded, not change it during an animation.

葬花如无物 2025-01-05 02:50:17

尼克的解决方案很好。

如果您想为滑动和调用相同的方法;点击按钮,您可以像这样调整 swipeRight:

- (void)goThrougtData:(id)sender
{
  if( [sender isKindOfClass:[UISwipeGestureRecognizer class]] ) {
    // swipe specific code
  }
  else if( [sender isKindOfClass:[UIButton class]] ) {
    // tap specific code
  }
  if ([questions hasPrevQuestion] == YES) {
    [self vorige:nil];
  }    
}

并在 init 方法中添加

[mySwipeRecognizer addTarget:self action:@selector(vorige:)];
[vorigeButton addTarget:self action:@selector(vorige:) forControlEvents:UIControlEventTouchUpInside];

Nick's solution is good.

if you want to call the same method for the swiping & the tap on your button, you can tweak your swipeRight like this:

- (void)goThrougtData:(id)sender
{
  if( [sender isKindOfClass:[UISwipeGestureRecognizer class]] ) {
    // swipe specific code
  }
  else if( [sender isKindOfClass:[UIButton class]] ) {
    // tap specific code
  }
  if ([questions hasPrevQuestion] == YES) {
    [self vorige:nil];
  }    
}

and in your init method, you add

[mySwipeRecognizer addTarget:self action:@selector(vorige:)];
[vorigeButton addTarget:self action:@selector(vorige:) forControlEvents:UIControlEventTouchUpInside];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文