gpt4 book ai didi

ios - 如何永久取消选择分段控制按钮中的段,直到再次单击

转载 作者:IT王子 更新时间:2023-10-29 07:49:27 25 4
gpt4 key购买 nike

我有一个包含 4 个段的 UISegmentedControl。当它被选中时,它应该保持选中状态。再次单击同一段时,它应该取消选择自己。如何实现?

最佳答案

由于 UISegmentedControl 仅在未选择的段被选中时才发送 Action ,因此您必须子类化 UISegmentedControl 以对其触摸处理进行微小的更改。我使用这个类:

@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 事件,即使该段已被选中。只需将当前索引保存在一个变量中并在操作中进行比较。如果两个索引匹配,则必须取消选择接触的段。

// _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: 期间更改。

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

@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 注意到的问题。

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 将此修复更改为如下所示:

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)
}
}
}
}

关于ios - 如何永久取消选择分段控制按钮中的段,直到再次单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17652773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com