2011-05-24 20 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

相關問題