SVSegmentedControl 是一个可定制的 UIControl
类,模仿 UISegmentedControl
但看起来像 UISwitch
。
注意:如果您的项目使用 ARC:您可能更好的使用原始的 SVSegmentedControl。
SVSegmentedControl.deARCed/SVSegmentedControl
文件夹拖拽到您的项目中。(见 /Demo
中的示例 Xcode 项目)
以最简单的形式,这是创建 SVSegmentedControl 实例的方法
segmentedControl = [[[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:@"Section 1", @"Section 2", nil]] autorelease];
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
为了定制滑块的显示,您必须通过 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,鸣谢将是受欢迎的。
目前,Slipp D.正在维护这个并行分叉的de-ARC-ification。如果这个库的内存管理有问题,在这个de-ARCed分叉中创建一个问题。如有疑问,请在那里创建问题,如果Slipp认为有必要,他将转交。