如何为按钮上的滑动和 TouchUpInside 提供两种不同的方法?

发布于 2024-11-09 12:34:59 字数 1330 浏览 0 评论 0原文

我有一个视图,其中有几个大按钮,我设置如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情癖 2024-11-16 12:34:59

我认为您不需要子类化 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>

确实有效,我自己试过了。希望有帮助! ;)

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! ;)

茶底世界 2024-11-16 12:34:59

看看 UIGestureRecognizer< /code> 文档和相关示例(eg) 。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文