自定义开关视图
/// If you want to have rounded dots and switch view or not, YES by default
@property (nonatomic, assign) BOOL rounded;
/// Width of the border, 1 by default
@property (nonatomic, assign) CGFloat borderWidth;
/// color of the border, white by default
@property (nonatomic, assign) UIColor* borderColor;
/// color of the background of the switch view, clearColor by default
@property (nonatomic, strong) UIColor* color;
/// color of the background of the switch view when switched, clearColor by default
@property (nonatomic, strong) UIColor* tintColor;
/// width and height of the dot, frameHeight-2 by default
@property (nonatomic, assign) CGFloat dotWeight;
/// color of the dot, white by default
@property (nonatomic, strong) UIColor* dotColor;
/// duration of the animation, 0.6 by default
@property (nonatomic, assign) NSTimerInterval animDuration;
/// delegate to be set
@property (nonatomic, weak) id<CMSwitchViewDelegate> delegate;
当点击开关视图或使用滑动手势移动到中部点之后时调用,并同时发送新的值给你
- (void)switchValueChanged:(id)sender andNewValue:(BOOL)value;
self.firstSwitch = [[CMSwitchView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-150, 100, 300, 50)];
self.firstSwitch.dotColor = [UIColor blueColor];
self.firstSwitch.color = [UIColor whiteColor];
self.firstSwitch.tintColor = [UIColor clearColor];
[self.view addSubview:self.firstSwitch];
self.secondSwitch = [[CMSwitchView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-100, self.firstSwitch.frame.size.height+self.firstSwitch.frame.origin.y+100, 200, 60)];
self.secondSwitch.dotColor = [UIColor whiteColor];
self.secondSwitch.color = [UIColor clearColor];
self.secondSwitch.tintColor = [UIColor clearColor];
self.secondSwitch.dotWeight = 20.f;
[self.view addSubview:self.secondSwitch];
self.thirdSwitch = [[CMSwitchView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-50, self.secondSwitch.frame.size.height+self.secondSwitch.frame.origin.y+100, 100, 30)];
self.thirdSwitch.animDuration = 2.f;
self.thirdSwitch.isRounded = NO;
[self.view addSubview:self.thirdSwitch];
CMSwitchView 可通过 CocoaPods 使用。要安装它,只需将以下行添加到 Podfile 中
pod "CMSwitchView"
Mycose, [email protected]
CMSwitchView 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。