测试已测试 | ✗ |
语种语言 | Obj-CObjective C |
许可协议 | MIT |
发布最新发布 | 2014年12月 |
由 Ernesto Rivera 维护。
SVSegmentedControl 是一个可定制的 UIControl
类,模仿 UISegmentedControl
的外观,但看起来像 UISwitch
。
重要提示:如果您项目不使用 ARC:您必须在目标设置 > 编译选项 > 编译源文件中添加 -fobjc-arc
编译器标志到 SVSegmentedControl.m
和 SVSegmentedThumb.m
。
SVSegmentedControl/SVSegmentedControl
文件夹拖到您的项目中。(见 /Demo
下的样例 Xcode 项目)
它的最简单形式,这样创建一个 SVSegmentedControl 实例
segmentedControl = [[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:@"Section 1", @"Section 2", nil]];
segmentedControl.changeHandler = ^(NSUInteger newIndex) {
// respond to index change
};
[self.view addSubview:segmentedControl];
您可以使用它的 frame
或 center
属性来定位
SVSegmentedControl 可以使用以下属性进行定制
@property (nonatomic, strong) NSArray *sectionTitles;
@property (nonatomic, strong) NSArray *sectionImages;
@property (nonatomic, readwrite) BOOL animateToInitialSelection; // default is NO
@property (nonatomic, readwrite) BOOL crossFadeLabelsOnDrag; // default is NO
@property (nonatomic, readwrite) BOOL mustSlideToChange; // default is NO - To make the control difficult to accidentally change, force the user to slide it
@property (nonatomic, readwrite) CGFloat minimumOverlapToChange; // default is 0.66 - Only snap to a new segment if the thumb overlaps it by this fraction
@property (nonatomic, readwrite) UIEdgeInsets touchTargetMargins; // default is UIEdgeInsetsMake(0, 0, 0, 0) - Enlarge touch target of control
@property (nonatomic, strong) UIColor *backgroundTintColor; // default is [UIColor colorWithWhite:0.1 alpha:1]
@property (nonatomic, retain) UIImage *backgroundImage; // default is nil
@property (nonatomic, readwrite) CGFloat height; // default is 32.0
@property (nonatomic, readwrite) UIEdgeInsets thumbEdgeInset; // default is UIEdgeInsetsMake(2, 2, 3, 2)
@property (nonatomic, readwrite) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsMake(0, 10, 0, 10)
@property (nonatomic, readwrite) CGFloat cornerRadius; // default is 4.0
@property (nonatomic, retain) UIFont *font; // default is [UIFont boldSystemFontOfSize:15]
@property (nonatomic, retain) UIColor *textColor; // default is [UIColor grayColor];
@property (nonatomic, strong) UIColor *innerShadowColor; // default is [UIColor colorWithWhite:0 alpha:0.8]
@property (nonatomic, retain) UIColor *textShadowColor; // default is [UIColor blackColor]
@property (nonatomic, readwrite) CGSize textShadowOffset; // default is CGSizeMake(0, -1)
它的滑块(SVSegmentedThumb
)也可以进行定制
@property (nonatomic, retain) UIImage *backgroundImage; // default is nil;
@property (nonatomic, retain) UIImage *highlightedBackgroundImage; // default is nil;
@property (nonatomic, retain) UIColor *tintColor; // default is [UIColor grayColor]
@property (nonatomic, assign) UIColor *textColor; // default is [UIColor whiteColor]
@property (nonatomic, assign) UIColor *textShadowColor; // default is [UIColor blackColor]
@property (nonatomic, readwrite) CGSize textShadowOffset; // default is CGSizeMake(0, -1)
@property (nonatomic, readwrite) BOOL shouldCastShadow; // default is YES (NO when backgroundImage is set)
@property (nonatomic, assign) CGFloat gradientIntensity; // default is 0.15
要定制滑块的 appearance,您必须通过 SVSegmentedControl 的 thumb
属性设置属性。例如,设置滑块的 tintColor
是通过
segmentedControl.thumb.tintColor = someColor;
您可以使用块处理程序响应用户值变化
segmentedControl.changeHandler = ^(NSUInteger newIndex) {
// respond to index change
};
如果您还没有爱上块,您仍然可以使用经典的 UIControl 方法
[mySegmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
提供以分号结尾的操作方法,因此可以访问发送对象
- (void)segmentedControlChangedValue:(SVSegmentedControl*)segmentedControl {
NSLog(@"segmentedControl did select index %i", segmentedControl.selectedIndex);
}
SVSegmentedControl 由 Sam Vermette 和 对项目做出贡献的人 提供。如果您有功能建议或错误报告,请通过发送拉取请求或在 创建新问题 来帮助。如果您在项目中使用 SVSegmentedControl,进行归因将会很有意义。