Objective C 语法:UISwipeGestureRecognizer 上的 addTarget
我目前正在开发一个应用程序,用户可以选择滑动浏览数据,或使用按钮浏览数据。我无法理解如何组合两段代码。
这是我用于滑动的代码:(
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
- (void)addTarget:(id)target action:(SEL)action
是相当于将按钮操作绑定到方法的代码,就像您从按钮按住 Ctrl 键拖动到 IBAction 时所做的那样界面生成器。因此,如果您的方法名为
vorige:
并且您希望在点击按钮时调用它,您会说:但我不知道您为什么要这样做,因为动画 - 通常在加载视图时设置按钮操作一次,而不是在动画期间更改它。
- (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: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.
尼克的解决方案很好。
如果您想为滑动和调用相同的方法;点击按钮,您可以像这样调整 swipeRight:
并在 init 方法中添加
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:
and in your init method, you add