简体   繁体   English

如何在按钮和 TouchUpInside 上使用两种不同的 SWIPE 方法?

[英]How to have two different methods for a SWIPE over a button and a TouchUpInside?

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.我希望能够触摸这些按钮然后触发 indexAction 方法,或者 - 如果识别到滑动 - 触发另一种方法。

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.我以为我将 UIButton 子类化以使滑动通过,但这并不是真正的解决方案,因为现在可以识别滑动和点击,因此两种方法都会触发。

Is there a way around this?有没有解决的办法? How can I prioritise the swiping and only allow the touchupinside if there was NO SWIPE?如果没有 SWIPE,我如何确定滑动的优先级并且只允许 touchupinside?

Any help would be very much appreciated.任何帮助将不胜感激。

Here is how I subclassed the 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 don't think that you need to subclass UIButton class.我认为您不需要继承 UIButton class。 Why don't you try something like this:你为什么不尝试这样的事情:

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];

Then you create the methods to handle taps and swipes on the button:然后创建方法来处理按钮上的点击和滑动:

// 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");

}

First of the three previous method is declared in a protocol, so don't forget to say that your class is implementing that protocol:前面三个方法中的第一个在协议中声明,所以不要忘记说您的 class 正在实现该协议:

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>

It works, I just tried it myself.有效,我自己也试过了。 Hope it helps;希望能帮助到你; ;) ;)

Take a look at the UIGestureRecognizer documentation and related examples ( eg ).查看UIGestureRecognizer文档和相关示例(例如)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM