【问题标题】:How do I subclass UISegmentedControl so that individual segments recognize a UILongPressGestureRecognizer?如何子类化 UISegmentedControl 以便各个段识别 UILongPressGestureRecognizer?
【发布时间】:2011-08-22 02:45:25
【问题描述】:

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

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

【问题讨论】:

    标签: ios uigesturerecognizer uisegmentedcontrol


    【解决方案1】:

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

    UILongPressGestureRecognizer* recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressGesture:)];
    recognizer.delegate = self;
    [mySegCtrl addGestureRecognizer:recognizer];
    [recognizer release];
    
    ...
    
    -(void)pressGesture:(UILongPressGestureRecognizer*)gesture
    {
        NSLog(@"pressGesture %@", gesture);
    }
    

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

    【讨论】:

      猜你喜欢
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 2021-12-29
      相关资源
      最近更新 更多