如何解决如何在按钮和TouchUpInside上有两种不同的SWIPE方法?
| 我有一个视图,其中有几个大按钮,它们的设置如下:SwipeButton* btn = [[[SwipeButton alloc] initWithFrame:CGRectMake(55,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
解决方法
我认为您不需要继承UIButton类。你为什么不尝试这样的事情:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:@\"picture_0.png\"];
[button setImage:image forState:UIControlStateNormal];
[button setFrame:CGRectMake((applicationFrame.size.width - image.size.width)/2,applicationFrame.size.height/2,image.size.width,image.size.height)];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[recognizer setDelegate:self];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[recognizer setDelegate:self];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
然后,创建用于处理按钮上的轻击和滑动的方法:
// Prevent recognizing touches on the slider
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UIButton class]]) {
return YES;
}
return NO;
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if ([recognizer direction] == UISwipeGestureRecognizerDirectionLeft) {
// Handle left swipe
NSLog(@\"left swipe\");
} else {
// Handle right swipe
NSLog(@\"right swipe\");
}
}
- (void)buttonTapped:(id)sender {
NSLog(@\"button pressed\");
}
协议中声明了前面三个方法中的第一个,因此不要忘了说您的类正在实现该协议:
@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
它有效,我自己尝试过。希望能帮助到你! ;)
,看一下UIGestureRecognizer
文档和相关示例(例如)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。