Skip to content

0xNSHuman/ORBSwitch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Another custom iOS switch control.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published