创建一个可两次点击的分段控件

  UISegmentdControl 类提供了一个多按钮的界面,用户可以从一组按钮中选择一个。该控件提供两种使用样式。

在正常的单选按钮样式模式下,一旦选中一个按钮,该按钮就一直处于选中状态。用户可以单击其他按钮,但它们

不能通过再次单击现有按钮来生成新事件。通过备用瞬时样式,用户可以多次单击每个按钮,但不存储当前选中项目的状态

它不突出显示最新选项。


代码: 创建响应第二次点击的分段控件子类


@class DoubleTapSegmentedControl;

@protocol DoubleTapSegmentedControlDelegate <NSObject>
- (void) performSegmentAction: (DoubleTapSegmentedControl *) aDTSC;
@end

@interface DoubleTapSegmentedControl : UISegmentedControl
{
    id <DoubleTapSegmentedControlDelegate> delegate;
}
@property (nonatomic, retain) id delegate;
@end

@implementation DoubleTapSegmentedControl
@synthesize delegate;

// Catch touches and trigger the delegate
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    if (self.delegate) [self.delegate performSegmentAction:self];
}
@end


@interface TestBedViewController : UIViewController <DoubleTapSegmentedControlDelegate>
@end

@implementation TestBedViewController
- (void) performSegmentAction: (DoubleTapSegmentedControl *) seg
{
    char *s[3] = {"One", "Two", "Three"};
    showAlert(@"Segment %s", s[seg.selectedSegmentIndex]);
}

- (void) viewDidLoad
{
    self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
    self.title = @"Twice Tappable Segments";
    
    // Build the control and set its delegate to this view controller
    DoubleTapSegmentedControl *seg = [[[DoubleTapSegmentedControl alloc] initWithItems:[@"One Two Three" componentsSeparatedByString:@" "]] autorelease];
    seg.center = CGPointMake(160.0f, 140.0f);
    seg.delegate = self;
    [self.view addSubview:seg];
}
@end





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用SwiftUI自定义iOS分段件可以通过以下步骤实现: 1. 创建一个新的SwiftUI View,命名为SegmentedControl。 2. 在SegmentedControl中定义一个枚举类型,用于表示分段件中的选项。 3. 在SegmentedControl中定义一个@Binding属性用于绑定选中的选项。 4. 在SegmentedControl中使用ForEach循环遍历所有的选项,并将它们显示在分段件中。 5. 在ForEach循环中,使用Button显示每一个选项,并在按钮的action中更新选中的选项。 6. 为分段件添加样式,例如设置选中的选项的背景色和字体颜色等。 下面是一个简单的示例代码: ```swift enum SegmentedOption: String, CaseIterable { case option1 case option2 case option3 } struct SegmentedControl: View { @Binding var selectedOption: SegmentedOption var body: some View { HStack { ForEach(SegmentedOption.allCases, id: \.self) { option in Button(action: { self.selectedOption = option }) { Text(option.rawValue) .foregroundColor(self.selectedOption == option ? .white : .black) .padding(.horizontal, 20) .padding(.vertical, 10) .background(self.selectedOption == option ? Color.blue : Color.gray) .cornerRadius(10) } } } } } ``` 在使用时,只需要将SegmentedControl添加到需要显示的View中,并将选中的选项绑定到某个属性即可。例如: ```swift struct ContentView: View { @State private var selectedOption: SegmentedOption = .option1 var body: some View { VStack { SegmentedControl(selectedOption: $selectedOption) Text("Selected option: \(selectedOption.rawValue)") } } } ``` 这样就可以在界面上显示一个自定义的iOS分段件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值