基于 UISwitch 和 UISegmentedControl 的自定义控件,使用 Objective-C 编写
DVSwitch 受 UISwitch 和 UISegmentedControl 启发。本代码的目标是:
滑块位于一个项目到另一个项目的一半,注意像素文本颜色的变化
DVSwitch *switcher = [[DVSwitch alloc] initWithStringsArray:@[@"First", @"Second"]];
switcher.frame = CGRectMake(20, 60, self.view.frame.size.width - 40, 34);
[self.view addSubview:switcher];
[switcher setPressedHandler:^(NSUInteger index) {
NSLog(@"Did switch to index: %lu", (unsigned long)index);
}];
可定制属性
UIColor *backgroundColor
- 控件的背景颜色UIColor *sliderColor
- 滑块颜色UIColor *labelTextColorInsideSlider
- 滑块上悬停时的文本颜色UIColor *labelTextColorOutsideSlider
- 滑块外部的文本颜色UIFont *font
- 控件中使用的字体CGFloat cornerRadius
- 控件和滑块的圆角半径CGFLoat sliderOffset
- 滑块和控制边缘之间的像素偏移量当用户点击或滑动控件时,handler 块会用触发元素的索引来调用。要设置它,请使用以下方法:
- (void)setPressedHandler:(void (^)(NSUInteger index))handler;
源代码包含带有几种不同类型开关的示例项目。
iOS 7.0 和 Xcode 6.0
控件可能在更早的版本上运行,但这未经过测试。
1.0.1 - 添加了不调用完成块而更改索引的方法。
1.0.0 - 初始版本。
我们欢迎任何对该项目的反馈或拉取请求。任何更改都应不改变已经工作的行为和API。欢迎添加新功能。
版本:1.0.1
许可证:[MIT](https://open-source.org.cn/licenses/MIT)
Stas Zhukovskiy
René Fouquet
Dmitry Volevodz