如何为按钮上的滑动和 TouchUpInside 提供两种不同的方法?
我有一个视图,其中有几个大按钮,我设置如下:
SwipeButton* btn = [[[SwipeButton alloc] initWithFrame:CGRectMake(55, 0, 300, 50)] autorelease];
btn.tag = k;
[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"titleBackground.png"]]
forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(indexAction:)
forControlEvents:UIControlEventTouchUpInside];
我希望能够触摸这些按钮,然后触发 indexAction 方法,或者 - 如果识别到滑动 - 触发另一个方法。
我以为我将 UIButton 子类化以实现滑动,但这并不是真正的解决方案,因为现在滑动和单击都被识别,所以这两种方法都会触发。
有办法解决这个问题吗?如果没有滑动,我怎样才能优先考虑滑动并且只允许内部触摸?
任何帮助将非常感激。
以下是我对 UIButton 进行子类化的方法:
#import "SwipeButton.h"
@implementation SwipeButton
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.superview touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[self.superview touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self.superview touchesEnded:touches withEvent:event];
}
@end
I have a view in which I have several large buttons which I set up like this:
SwipeButton* btn = [[[SwipeButton alloc] initWithFrame:CGRectMake(55, 0, 300, 50)] autorelease];
btn.tag = k;
[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"titleBackground.png"]]
forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(indexAction:)
forControlEvents:UIControlEventTouchUpInside];
I would like to be able to either touch these buttons and then fire the indexAction method, or - if a swipe was recognised - to fire another method.
I thought I subclass UIButton to have swipes come through, but this isn't really a solution as now both swipes and clicks are recognised, so BOTH methods fire.
Is there a way around this? How can I prioritise the swiping and only allow the touchupinside if there was NO SWIPE?
Any help would be very much appreciated.
Here is how I subclassed the UIButton:
#import "SwipeButton.h"
@implementation SwipeButton
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.superview touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[self.superview touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self.superview touchesEnded:touches withEvent:event];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您不需要子类化 UIButton 类。为什么不尝试这样的事情:
然后创建处理按钮上的点击和滑动的方法:
前面三个方法中的第一个是在协议中声明的,所以不要忘记说您的类正在实现该协议:
确实有效,我自己试过了。希望有帮助! ;)
I don't think that you need to subclass UIButton class. Why don't you try something like this:
Then you create the methods to handle taps and swipes on the button:
First of the three previous method is declared in a protocol, so don't forget to say that your class is implementing that protocol:
It works, I just tried it myself. Hope it helps! ;)
看看
UIGestureRecognizer< /code> 文档
和相关示例(eg) 。
Take a look at the
UIGestureRecognizer
documentation and related examples (e.g.).