【发布时间】:2011-05-24 08:47:00
【问题描述】:
我有一个视图,其中有几个大按钮,我设置如下:
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 的子类,可以让滑动通过,但这并不是真正的解决方案,因为现在可以识别滑动和点击,所以这两种方法都会触发。
有没有办法解决这个问题?如果没有 SWIPE,我如何确定滑动的优先级并只允许 touchupinside?p>
非常感谢任何帮助。
这是我对 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
【问题讨论】:
-
你试过UIGestureRecognizer类吗?
-
@Nick Weaver:谢谢,尼克。我没有使用 UIGestureRecognizer,因为我想自定义滑动需要多长时间。因此,我使用 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event ... 但我想使用 GestureRecognizer 也没有害处。
-
为什么投反对票?我想这是一个愚蠢的问题,但实际上,我努力想了想。
标签: iphone objective-c cocoa-touch uibutton