【问题标题】:UISegmentedControl unselect segment by pressing it againUISegmentedControl 再次按下选择段
【发布时间】:2015-09-14 12:35:23
【问题描述】:

如何通过再次按下同一段来取消选择 UISegmented 控件中的给定段?

例如按下段 0,它将被选中并保持突出显示。再次按下段 0,它将变为未选中且未突出显示。

该控件仅触发 UIControlEventValueChanged 事件。其他事件似乎不适用于它。

有一个属性 'momentary' 当设置为 YES 时几乎允许上述行为,除了突出显示只是暂时的。当 momentary=YES 按下同一段两次会导致两个 UIControlEventValueChanged 事件,但是当 momentary=NO 时,只有第一次按下给定段会导致 UIControlEventValueChanged 事件被触发。 IE。对同一段的后续按下不会触发 UIControlEventValueChanged 事件。

【问题讨论】:

  • 总会有一个段被选中,不能全部取消。使用 UIButton 更好地创建您的段控件。
  • 恐怕我不得不同意@iphonic。如果您对此行为有很好的用例,请提交增强请求 - 这听起来确实是一件很酷的事情 - 但就目前而言,我认为您只是试图破坏分段控件的行为,并且你不会成功的。

标签: ios uisegmentedcontrol


【解决方案1】:

基于@protspace 的回答,更简单的方法,不用在@IBAction写任何东西:

class DeselectableSegmentedControl: UISegmentedControl {
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let previousSelectedSegmentIndex = selectedSegmentIndex

        super.touchesEnded(touches, with: event)

        if previousSelectedSegmentIndex == selectedSegmentIndex {
            let touch = touches.first!
            let touchLocation = touch.location(in: self)
            if bounds.contains(touchLocation) {
                selectedSegmentIndex = UISegmentedControl.noSegment
                sendActions(for: .valueChanged)
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    不完全符合您的要求,但我正在寻找类似的功能并决定这样做。

    将双击手势添加到设置 selectedSegmentIndex 的 UISegmentedControl

    当你初始化你的 UISegmentedControl。

    let doubleTap = UITapGestureRecognizer(target: self, action #selector(YourViewController.doubleTapToggle))
    doubleTap.numberOfTapsRequired = 2
    yourSegmentedControl.addGestureRecognizer(doubleTap)
    

    取消选择片段的功能

    @objc func doubleTapToggle () {
        yourSegmentedControl.selectedSegmentIndex = -1
        yourSegmentedControl.sendActions(for: valueChanged)
    }
    

    【讨论】:

      【解决方案3】:

      你可以继承 UISegmentedControl:

      斯威夫特 3

      class ReselectableSegmentedControl: UISegmentedControl {
      
          override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
              let previousSelectedSegmentIndex = self.selectedSegmentIndex
      
              super.touchesEnded(touches, withEvent: event)
      
              if previousSelectedSegmentIndex == self.selectedSegmentIndex {
                  if let touch = touches.first {
                      let touchLocation = touch.locationInView(self)
                      if CGRectContainsPoint(bounds, touchLocation) {
                          self.sendActionsForControlEvents(.ValueChanged)
                      }
                  }
              }
          }
      
      
      }
      

      斯威夫特 4

      override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
          let previousSelectedSegmentIndex = self.selectedSegmentIndex
      
          super.touchesEnded(touches, with: event)
      
          if previousSelectedSegmentIndex == self.selectedSegmentIndex {
              let touch = touches.first!
              let touchLocation = touch.location(in: self)
              if bounds.contains(touchLocation) {
                  self.sendActions(for: .valueChanged)
              }
          }
      }
      

      然后

       @IBAction func segmentChanged(sender: UISegmentedControl) {
          if (sender.selectedSegmentIndex == selectedSegmentIndex) {
              sender.selectedSegmentIndex =  UISegmentedControlNoSegment;
              selectedSegmentIndex = UISegmentedControlNoSegment;
          }
          else {
              selectedSegmentIndex = sender.selectedSegmentIndex;
          }
      }
      

      【讨论】:

      • 我已经尝试了六种建议,这是唯一有效的。
      • 注意:if let touch = touches.first { 过于谨慎——touches 始终包含 1 个或多个 UITouches,因此 let touch = touches.first! 将始终成功。否则,很好的答案。干杯
      • @protspace,你能解释一下为什么我们需要检查触摸是否在视图的范围内吗?这不是给定的吗,因为事件首先被发送到视图?
      【解决方案4】:

      UISegmentedControl 可以通过 Swift 使用 @IBOutlet weak var mySegmentedControl: UISegmentedControl! 和 mySegmentedControl.selectedSegmentIndex = -1 以编程方式取消选择。至于检测段的触摸,我不知道。希望其他人能给出这个答案。

      【讨论】:

      • 看到这个答案here
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      相关资源
      最近更新 更多