如何子类化 UISegmentedControl 以便各个段识别 UILongPressGestureRecognizer?

发布于 2024-11-30 17:26:41 字数 454 浏览 0 评论 0原文

首先 这个问题有助于我理解如何对 UIButton 进行子类化以实现长按。我想对 UISegmentedControl 做同样的事情,但是我不知道如何能够识别哪个段被按下,因为 UISegmentedControl 确实允许直接访问它的段(UISegmentedControl.h 将它们显示为私有)。我可以自定义一些 UIButton,使其看起来像 UISegmentedControl,但是我还必须实现瞬时切换逻辑。这没什么大不了的,但子类化 UISegmentedControl 对我来说似乎更干净。

顺便说一句,我使用此控件来模仿收音机的预设控件:点击以转到已保存的电台并按住以将当前电台分配给该段。

First off this question has been helpful in my understanding of how to subclass UIButton for long presses. I would like to do the same for UISegmentedControl, however I don't see how I would be able to identify which segment was held down since UISegmentedControl does allow direct access to it's segments (UISegmentedControl.h shows them as private). I could just customize a few UIButtons to look like an UISegmentedControl however I would also have to implement the momentary switch logic. Which wouldn't be a big deal but subclassing UISegmentedControl seems cleaner to me.

BTW, I'm using this control to imitate a radio's preset controls: tap to go to a saved station and hold to assign the current station to that segment.

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

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

发布评论

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

评论(1

若言繁花未落 2024-12-07 17:26:41

我在没有子类化的情况下尝试了这个,它似乎有效。

UILongPressGestureRecognizer* recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressGesture:)];
recognizer.delegate = self;
[mySegCtrl addGestureRecognizer:recognizer];
[recognizer release];

...

-(void)pressGesture:(UILongPressGestureRecognizer*)gesture
{
    NSLog(@"pressGesture %@", gesture);
}

长按首先选择片段,然后触发手势。如果您没有收到回调,请检查我的代码 - 我被卡住了一段时间,因为我没有设置 recognizer.delegate=self。

I tried this without subclassing and it seems to work.

UILongPressGestureRecognizer* recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressGesture:)];
recognizer.delegate = self;
[mySegCtrl addGestureRecognizer:recognizer];
[recognizer release];

...

-(void)pressGesture:(UILongPressGestureRecognizer*)gesture
{
    NSLog(@"pressGesture %@", gesture);
}

Long press first selects the segment then fires the gesture. If you aren't getting the callback check my code - I got stuck for a while because I wasn't setting recognizer.delegate=self.

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