开发者

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.

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


I don't think that you need to subclass 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:

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>

It works, I just tried it myself. Hope it helps! ;)


Take a look at the UIGestureRecognizer documentation and related examples (e.g.).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜