2011-05-24 45 views
0

我有,我有几个大按钮,我设置了这样的观点:如何通过按钮和TouchUpInside有两种不同的SWIPE方法?

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,只允许touchup内侧?

任何帮助将非常感激。

这里是我的子类的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 
+1

您是否尝试过[UIGestureRecognizer](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html)类? – 2011-05-24 08:52:30

+0

@尼克韦弗:谢谢,尼克。我没有使用UIGestureRecognizer,因为我想定制刷卡需要多长时间。因此,我使用 - (void)touchesEnded:(NSSet *)触及事件:(UIEvent *)事件...但我想使用GestureRecognizer也没有什么坏处。 – 2011-05-24 12:20:07

+0

为什么要投票?我想这是一个愚蠢的问题,但真的,我试图认真思考。 – 2011-05-24 12:20:50

回答

3

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

它的工作原理,我只是尝试过自己。希望能帮助到你! ;)

+0

非常感谢...!真的很有帮助。我没有考虑使用GestureRecognizer - (void)touchesEnded:(NSSet *)触及事件:(UIEvent *)事件来确定滑动长度(并根据我的需要对其进行自定义)。但显然,我想,使用两者都没有错...... – 2011-05-24 12:22:36

+1

不客气! – singingAtom 2011-05-24 12:30:17

+0

工程就像一个魅力...再次感谢。但我真的不知道如何设置分钟。所需的滑动长度。理想情况下,我想将其设置为10像素,并将方差设置为15像素,但我无法看到使用UIGestureRecognizer执行此操作的方法。 – 2011-05-24 12:40:38

相关问题