2011-07-15 144 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中的按钮移动到任何其他位置。我怎样才能做到这一点。 –

相关问题