Skip to content

Files

Latest commit

 

History

History
72 lines (52 loc) · 2.51 KB

README.md

File metadata and controls

72 lines (52 loc) · 2.51 KB

ORBSwitch License: Zlib Version: 1.0

ORBSwitch is a simple iOS-style switch with a few customization options:

  • Squared or Rounded Pre-defined styles with colors customization;
  • Custom style supporting any background and switch knob image;
  • Controllable animation events and dynamic property changes;

demo

Usage

Pre-defined style switch creation

Create Square or Round Switch

ORBSwitch *squareSwitch = [[ORBSwitch alloc] initWithType:ORBSwitchSquare frame:CGRectMake(0, 0, 200, 120)];

ORBSwitch *roundSwitch = [[ORBSwitch alloc] initWithType:ORBSwitchRound frame:CGRectMake(0, 0, 200, 120)];

Set required properties and add switch as subview

switch.knobColor = [UIColor orangeColor];
switch.inactiveBackgroundColor = [UIColor lightGrayColor];
switch.activeBackgroundColor = [UIColor darkGrayColor];

switch.delegate = (id<ORBSwitchDelegate>)self;
[self.view addSubview:switch];

Custom style switch creation

Set any image for switch knob + background image for both ON and OFF switch states

ORBSwitch *customSwitch = [[ORBSwitch alloc] initWithCustomKnobImage:myKnobUIImage inactiveBackgroundImage:myOffStateBackgroundUIImage activeBackgroundImage:myOnStateBackgroundUIImage frame:CGRectMake(0, 0, 200, 120)];

customSwitch.delegate = (id<ORBSwitchDelegate>)self;
[self.view addSubview:customSwitch];

Universal modifications

You can change optional properties for both pre-defined and custom styles

switch.knobRelativeHeight = 0.8f;

Delegate Methods

- (void)orbSwitchToggled:(ORBSwitch *)switchObj withNewValue:(BOOL)newValue {
    NSLog(@"Switch toggled: new state is %@", (newValue) ? @"ON" : @"OFF");
}

- (void)orbSwitchToggleAnimationFinished:(ORBSwitch *)switchObj {
    if (switchObj == _switch4) {
        [switchObj setCustomKnobImage:[UIImage imageNamed:(switchObj.isOn) ? @"mario_l" : @"mario_r"]
              inactiveBackgroundImage:[UIImage imageNamed:@"mario_bg"]
                activeBackgroundImage:[UIImage imageNamed:@"mario_bg"]];
    }
}

ToDo's

  • Document code;
  • Extend customization options;

License

ORBSwitch is released under the permissive zlib License. See the LICENSE file.