UIButton 的触摸检测添加为 UIScrollView 中的子视图

发布于 2024-11-23 16:58:49 字数 157 浏览 1 评论 0原文

我有一个带有许多按钮的滚动视图。我想检测滚动视图内 UIButton 的触摸,但无法获取它们。我尝试通过子类化 uiscrollview 和 uibutton 但失败了。如果用户按住该按钮很短时间(例如 2 秒),我想检测按钮上的触摸。然后我想将按钮拖动到用户可以在滚动视图上拖动它的位置。请帮助我。

I have a scroll view with many buttons. I want to detect the touches on UIButton inside scrollView but not been able to get them. I tried by subclassing uiscrollview, and uibutton
but failed. I want to detect touches on button if user holds that button for a short time, say 2 sec. And then i want to drag the button to place where user can drag it on scroll view. Plz help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2024-11-30 16:58:49

你不需要子类化。

尝试将 UILongPressGestureRecognizer 附加到您的 UIButton

请查看此处了解更多信息信息。

iOS 3.2 开始提供手势识别器,使所有与手势相关的事情变得非常简单。 在这里您可以找到教程。

如果您想支持以前的版本,则必须采用不同的方法:

  1. 向按钮添加 UIControlEventTouchUpInsideUIControlEventTouchDown 操作;

  2. 在 touchDown 处理程序中开始计时(用当前时间设置一个变量);

  3. 在 touchUp 处理程序中停止计时;测量差异,如果高于阈值,则触发操作。

如果这不起作用,请提供更多信息来说明为什么不起作用。

You don't need to subclass.

Try to attach to your UIButton a UILongPressGestureRecognizer.

Have a look here for more info.

Gesture recognizers are available from iOS 3.2 and make very easy all things related to gestures. Here you can find a tutorial.

If you want to support previous versions, you have to resort to a different method:

  1. add UIControlEventTouchUpInside and UIControlEventTouchDown actions to your button;

  2. in the touchDown handler start counting time (set a variable with current time);

  3. in touchUp handler stop counting time; measure the difference and if is above your threshold, fire you action.

If this does not work, provide some more information as to why it doesn't, please.

花伊自在美 2024-11-30 16:58:49

您可以为 scrollView 上的每个按钮设置一个 tag,然后像 @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)

You can set a tag for each button on the scrollView, then like @sergio said add a UILongPressGestureRecognizer(or a uicontrolevent) to each button, so when you are setting the pages in the scrollview you could add:

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

or

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

and in your action..

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

or

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

(obviously add UIGestureRecognizerDelegate on your .h)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文