2012-02-13 37 views
1

我有2個按鈕與 「UILongPressGestureRecognizer」,要做到這一點,我做的:UILongPressGestureRecognizer有不同的按鈕

Fot的按鈕1:

-(IBAction)seleccionar46:(id)sender{ 
UILongPressGestureRecognizer *longpressGesture =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)]; 
longpressGesture.minimumPressDuration = 3; 
[longpressGesture setDelegate:self]; 
[self.button1 addGestureRecognizer:longpressGesture];} 

對於按鈕2:

-(IBAction)seleccionar46:(id)sender{ 
UILongPressGestureRecognizer *longpressGesture =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)]; 
longpressGesture.minimumPressDuration = 3; 
[longpressGesture setDelegate:self]; 
[self.button2 addGestureRecognizer:longpressGesture];} 

而且在「longpressGesture」我需要區分button1和button2,但我不能這樣做...

- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer{//Here i need do the differentation} 

謝謝大家!

此致敬禮。

回答

6

你可以做的是使用UIGestureRecognizerview屬性,所以如果你保存對兩個按鈕的引用,你可以檢查是否相等。

所以,如果你有

@interface blah 
{ 
    UIButton *buttonOne; 
    UIButton *buttonTwo; 
} 

那麼你識別器添加到您可以在處理程序

if(gestureRecognizer.view==buttonOne) 
{ 
    //do stuff for button one 
} 
else if(gestureRecognizer.view==buttonTwo) 
{ 
    //do stuff for button two 
} 

希望它有助於

+0

感謝朋友做的按鈕!我需要保存名稱NSSttring中的按鈕...我該怎麼做? – 2012-02-13 19:10:18

+0

爲什麼這樣做?... – Daniel 2012-02-13 19:17:14

+0

因爲按鈕的名稱與一個圖像的名稱相同...而當我按下按鈕3秒鐘時,它會顯示與按鈕相關的圖像...我希望你可以不言而喻...感謝所有丹尼爾 – 2012-02-13 19:21:38

相關問題