• <tfoot id='JGjuU'></tfoot>

    1. <small id='JGjuU'></small><noframes id='JGjuU'>

        <bdo id='JGjuU'></bdo><ul id='JGjuU'></ul>
      <i id='JGjuU'><tr id='JGjuU'><dt id='JGjuU'><q id='JGjuU'><span id='JGjuU'><b id='JGjuU'><form id='JGjuU'><ins id='JGjuU'></ins><ul id='JGjuU'></ul><sub id='JGjuU'></sub></form><legend id='JGjuU'></legend><bdo id='JGjuU'><pre id='JGjuU'><center id='JGjuU'></center></pre></bdo></b><th id='JGjuU'></th></span></q></dt></tr></i><div id='JGjuU'><tfoot id='JGjuU'></tfoot><dl id='JGjuU'><fieldset id='JGjuU'></fieldset></dl></div>
      <legend id='JGjuU'><style id='JGjuU'><dir id='JGjuU'><q id='JGjuU'></q></dir></style></legend>

      1. 如何在分段控制按钮中永久取消选择分段,直到再次单击它

        时间:2023-07-07
          <bdo id='opEp9'></bdo><ul id='opEp9'></ul>

          <small id='opEp9'></small><noframes id='opEp9'>

        • <tfoot id='opEp9'></tfoot>
              <tbody id='opEp9'></tbody>
          1. <i id='opEp9'><tr id='opEp9'><dt id='opEp9'><q id='opEp9'><span id='opEp9'><b id='opEp9'><form id='opEp9'><ins id='opEp9'></ins><ul id='opEp9'></ul><sub id='opEp9'></sub></form><legend id='opEp9'></legend><bdo id='opEp9'><pre id='opEp9'><center id='opEp9'></center></pre></bdo></b><th id='opEp9'></th></span></q></dt></tr></i><div id='opEp9'><tfoot id='opEp9'></tfoot><dl id='opEp9'><fieldset id='opEp9'></fieldset></dl></div>
          2. <legend id='opEp9'><style id='opEp9'><dir id='opEp9'><q id='opEp9'></q></dir></style></legend>

                • 本文介绍了如何在分段控制按钮中永久取消选择分段,直到再次单击它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个有 4 个段的 UISegmentedControl.当它被选中时,它应该保持选中状态.当再次单击同一段时,它应该取消选择自身.如何做到这一点?

                  I have a UISegmentedControl with 4 segments. When it is selected, it should maintain the selected state. When the same segment is clicked again, it should deselect itself. How to achieve this?

                  推荐答案

                  由于 UISegmentedControl 只在未选中段被选中时发送动作,所以你必须继承 UISegmentedControl对其触摸处理进行微小的更改.我使用这个类:

                  Since UISegmentedControl only sends an action if a not selected segment is selected, you have to subclass UISegmentedControl to make a tiny change in its touch handling. I use this class:

                  @implementation MBSegmentedControl
                  
                  // this sends a value changed event even if we reselect the currently selected segment
                  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
                      NSInteger current = self.selectedSegmentIndex;
                      [super touchesBegan:touches withEvent:event];
                      if (current == self.selectedSegmentIndex) {
                          [self sendActionsForControlEvents:UIControlEventValueChanged];
                      }
                  }
                  
                  @end
                  

                  现在您将获得 UIControlEventValueChanged 事件,即使该段已被选中.只需将当前索引保存在变量中并在操作中进行比较.如果两个索引匹配,您必须取消选择触摸的段.

                  Now you will get UIControlEventValueChanged events even if the segment is already selected. Simply save the current index in a variable and compare it in the action. If the two indexes match you have to unselect the touched segment.

                  // _selectedSegmentIndex is an instance variable of the view controller
                  
                  - (void)viewWillAppear:(BOOL)animated {
                      [super viewWillAppear:animated];
                      _selectedSegmentIndex = self.segment.selectedSegmentIndex;
                  }
                  
                  - (IBAction)segmentChanged:(UISegmentedControl *)sender {
                      if (sender.selectedSegmentIndex == _selectedSegmentIndex) {
                          NSLog(@"Segment %d deselected", sender.selectedSegmentIndex);
                          sender.selectedSegmentIndex =  UISegmentedControlNoSegment;
                          _selectedSegmentIndex = UISegmentedControlNoSegment;
                      }
                      else {
                          NSLog(@"Segment %d selected", sender.selectedSegmentIndex);
                          _selectedSegmentIndex = sender.selectedSegmentIndex;
                      }
                  }
                  

                  <小时>

                  iOS 7 更改了 UISegmentedControl 的触摸处理方式.selectedSegmentIndex 现在在 touchesEnded: 期间更改.

                  所以更新后的子类应该是这样的:

                  So the updated Subclass should look like this:

                  @implementation MBSegmentedControl
                  
                  + (BOOL)isIOS7 {
                      static BOOL isIOS7 = NO;
                      static dispatch_once_t onceToken;
                      dispatch_once(&onceToken, ^{
                          NSInteger deviceSystemMajorVersion = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] integerValue];
                          if (deviceSystemMajorVersion >= 7) {
                              isIOS7 = YES;
                          }
                          else {
                              isIOS7 = NO;
                          }
                      });
                      return isIOS7;
                  }
                  
                  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
                      NSInteger previousSelectedSegmentIndex = self.selectedSegmentIndex;
                      [super touchesBegan:touches withEvent:event];
                      if (![[self class] isIOS7]) {
                          // before iOS7 the segment is selected in touchesBegan
                          if (previousSelectedSegmentIndex == self.selectedSegmentIndex) {
                              // if the selectedSegmentIndex before the selection process is equal to the selectedSegmentIndex
                              // after the selection process the superclass won't send a UIControlEventValueChanged event.
                              // So we have to do this ourselves.
                              [self sendActionsForControlEvents:UIControlEventValueChanged];
                          }
                      }
                  }
                  
                  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
                      NSInteger previousSelectedSegmentIndex = self.selectedSegmentIndex;
                      [super touchesEnded:touches withEvent:event];
                      if ([[self class] isIOS7]) {
                          // on iOS7 the segment is selected in touchesEnded
                          if (previousSelectedSegmentIndex == self.selectedSegmentIndex) {
                              [self sendActionsForControlEvents:UIControlEventValueChanged];
                          }
                      }
                  }
                  
                  @end
                  

                  <小时>

                  Swift 2.2 版本,修复了 Grzegorz 注意到的问题.


                  Swift 2.2 version, fixed the problem Grzegorz noticed.

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

                  <小时>

                  Swift 3.0 对此进行了修改,如下所示:


                  Swift 3.0 changes the fix for this to look like the following:

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

                  这篇关于如何在分段控制按钮中永久取消选择分段,直到再次单击它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:禁用的 UIButton 不褪色或变灰 下一篇:UIButton TouchUpInside 触摸位置

                  相关文章

                    <tfoot id='WKrzX'></tfoot>
                      <bdo id='WKrzX'></bdo><ul id='WKrzX'></ul>

                    <small id='WKrzX'></small><noframes id='WKrzX'>

                    1. <legend id='WKrzX'><style id='WKrzX'><dir id='WKrzX'><q id='WKrzX'></q></dir></style></legend>
                    2. <i id='WKrzX'><tr id='WKrzX'><dt id='WKrzX'><q id='WKrzX'><span id='WKrzX'><b id='WKrzX'><form id='WKrzX'><ins id='WKrzX'></ins><ul id='WKrzX'></ul><sub id='WKrzX'></sub></form><legend id='WKrzX'></legend><bdo id='WKrzX'><pre id='WKrzX'><center id='WKrzX'></center></pre></bdo></b><th id='WKrzX'></th></span></q></dt></tr></i><div id='WKrzX'><tfoot id='WKrzX'></tfoot><dl id='WKrzX'><fieldset id='WKrzX'></fieldset></dl></div>