2011-07-15 137 views

回答

4

你不需要子類。

嘗試附加到您的UIButton a UILongPressGestureRecognizer

查看更多信息here

手勢識別器可以從iOS 3.2中獲得,並且可以非常方便地與手勢相關的所有事情。 Here你可以找到一個教程。

如果你想支持以前的版本中,你不得不求助於不同的方法:

  1. 添加UIControlEventTouchUpInsideUIControlEventTouchDown行動,以您的按鈕;

  2. 在touchDown處理程序中開始計時的時間(用當前時間設置一個變量);

  3. in touchUp handler停止計數時間;衡量差異,如果超過了你的門檻,就開始行動。

如果這不起作用,請提供一些關於它爲什麼不行的信息。

+0

感謝您的答覆,但後來長按之後,我需要爲用戶拖動來移動按鈕上的任何空間,滾動視圖。有什麼可以做到這一點。 –

+1

在上面鏈接的教程中(http://www.icodeblog.com/2010/10/14/working-with-uigesturerecognizers/),請轉到「實施移動」部分,您將找到工作代碼。 – sergio

+0

感謝您的幫助。 –

1

您可以設置每個按鈕tagscrollView,然後像@sergio說添加UILongPressGestureRecognizer(或uicontrolevent)每個按鈕,所以當你在設置滾動視圖的頁面,你可以添加:

[button addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventTouchUpInside]; 

UILongPressGestureRecognizer *twoSecPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(someAction:)]; 
      [twoSecPress setMinimumPressDuration:2]; 
      [button addGestureRecognizer:twoSecPress]; 
      [twoSecPress release]; 

,並在你的行動..

-(IBAction)someAction:(id)sender{ 
     UIButton *button=(UIButton*)sender; 
      if(button.tag==YOUR_TAG){ 
      //do something 
    } 
} 

-(void)someAction:(UILongPressGestureRecognizer *)recognizer { 
    if (recognizer.state == UIGestureRecognizerStateBegan) { 
      if ([recognizer.view isKindOfClass:[UIButton class]]) { 
       UIButton *tmpButt=(UIButton *)recognizer.view; 
       NSLog(@"%d", tmpButt.tag); 
    } 
} 

(顯然您的.h添加UIGestureRecognizerDelegate)

+1

我也想通過拖動長按按鈕將scrollView中的按鈕移動到任何其他位置。我怎樣才能做到這一點。 –

相關問題