【问题标题】:How to have two different methods for a SWIPE over a button and a TouchUpInside?如何在按钮和 TouchUpInside 上使用两种不同的 SWIPE 方法?
【发布时间】: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


【解决方案1】:

我认为您不需要继承 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>

它有效,我只是自己尝试过。希望能帮助到你! ;)

【讨论】:

  • 非常感谢...!真的很有帮助。我没有考虑 GestureRecognizer,因为我使用 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 来确定滑动长度(并根据我的需要进行自定义)。但显然,我想,两者都用没有错……
  • 像魅力一样工作......所以再次感谢。但我真的不知道如何设置分钟。所需的滑动长度。理想情况下,我想将其设置为 10 像素,将方差设置为 15 像素,但我看不到使用 UIGestureRecognizer 执行此操作的方法。
  • 如果使用 UISwipeGestureRecognizer,您无法设置最小滑动长度。为什么你还需要这样的东西?
  • 我希望滑动响应非常好。以照片应用程序为例。如果您只用手指轻扫一点(大约 5 像素),图片就会移动。如果您想立即获得反馈,我想自定义所需的滑动长度会非常有用。我有这样的方法,但正如我所说,我正在使用 touchesEnded 然后测量触摸起点和终点之间的差异 - 从而确定是否达到了所需的滑动长度阈值。
【解决方案2】:

查看UIGestureRecognizer documentation 和相关示例 (e.g.)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    相关资源
    最近更新 更多