Code Monkey home page Code Monkey logo

orbswitch's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.