BEMCheckBox

repository·master·Indexed 25 days ago

https://github.com/boris-em/bemcheckbox

An open source iOS library for creating customizable and animated checkboxes. It functions similarly to UISwitch and includes BEMCheckBoxGroup for radio button functionality. The library supports various animation types, appearance customization via properties, and a delegate protocol for responding to checkbox events.

Tokens
1.1K
Snippets
3
Records
6
Agent score
33%

What's inside BEMCheckBox

  1. Setup BEMCheckBox in your project

    master

    BEMCheckBox is modeled after UISwitch. To use it, first import the header in your view controller:

    #import "BEMCheckBox.h"

    Programmatic Initialization

    Initialize the checkbox in your implementation (e.g., viewDidLoad):

    BEMCheckBox *myCheckBox = [[BEMCheckBox alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [self.view addSubview:myCheckBox];

    Interface Builder Initialization

    1. Drag a UIView into your UIViewController.
    2. Change the class of the UIView to BEMCheckBox.
    3. Use the Attributes Inspector to configure customizable properties.
    #import "BEMCheckBox.h"
    
    BEMCheckBox *myCheckBox = [[BEMCheckBox alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [self.view addSubview:myCheckBox];
  2. Install BEMCheckBox

    master

    You can install BEMCheckBox using several dependency managers or manually:

    • Swift Package Manager: Add https://github.com/Boris-Em/BEMCheckBox as a dependency in your Package.swift or via Xcode's 'Add Package Dependency' menu.
    • CocoaPods: Add pod 'BEMCheckBox' to your Podfile.
    • Carthage: Add the repository to your Cartfile and run carthage update. Drag the resulting BEMCheckBox.framework into your Xcode project.
    • Manual Installation: Drag and drop the Classes folder into your Xcode project. Ensure you check the "Copy items into destination group's folder" box.
    # CocoaPods
    pod 'BEMCheckBox'
  3. Use BEMCheckBoxGroup for radio button functionality

    master

    You can group multiple BEMCheckBox instances to behave like radio buttons, where only one checkbox in the group can be selected at a time. You can also require that the group always has a selection.

    self.group = [BEMCheckBoxGroup groupWithCheckBoxes:@[self.checkBox1, self.checkBox2, self.checkBox3]];
    self.group.selectedCheckBox = self.checkBox2; // Optionally set pre-selection
    self.group.mustHaveSelection = YES; // If YES, the group must always have one selection
    
    // Get the current selection
    BEMCheckBox *selection = self.group.selectedCheckBox;
    
    // Manually update selection
    self.group.selectedCheckBox = self.checkBox1;
    self.group = [BEMCheckBoxGroup groupWithCheckBoxes:@[self.checkBox1, self.checkBox2, self.checkBox3]];
    self.group.selectedCheckBox = self.checkBox2;
    self.group.mustHaveSelection = YES;
    
    BEMCheckBox *selection = self.group.selectedCheckBox;
    self.group.selectedCheckBox = self.checkBox1;
  4. Implement BEMCheckBoxDelegate

    master

    To respond to checkbox events, conform to the BEMCheckBoxDelegate protocol. It provides two optional methods:

    • didTapCheckBox:: Called every time the checkbox is tapped, after the on property is updated but before animations finish.
    • animationDidStopForCheckBox:: Called when the checkbox finishes its animation.
  5. Control checkbox state (on/off)

    master

    Use the on property or the setOn:animated: method to manage the checkbox state.

    • on: A boolean property to get or set the state without animation. Defaults to NO.
    • setOn:animated:: Sets the state with an optional animation.
    • reload: Redraws the entire checkbox while maintaining the current on value.
  6. Customize BEMCheckBox appearance

    master

    BEMCheckBox is customized via properties.

    Appearance Properties:

    • lineWidth (CGFloat): Width of the check mark and box lines. Defaults to 2.0.
    • hideBox (BOOL): If YES, the box is hidden, leaving only the check mark. Defaults to NO.
    • boxType (BEMBoxType): The shape of the box. Defaults to BEMBoxTypeCircle.
    • tintColor (UIColor): Color of the box when off.
    • onCheckColor (UIColor): Color of the check mark when on.
    • onFillColor (UIColor): Color of the inside of the box when on.
    • onTintColor (UIColor): Color of the line around the box when on.

    Animation Properties:

    • animationDuration (CGFloat): Duration of animations in seconds. Defaults to 0.5.
    • onAnimationType (BEMAnimationType): Animation type used when checking. Defaults to BEMAnimationTypeStroke.
    • offAnimationType (BEMAnimationType): Animation type used when unchecking. Defaults to BEMAnimationTypeStroke.

    Available BEMAnimationType values:

    • BEMAnimationTypeStroke
    • BEMAnimationTypeFill
    • BEMAnimationTypeBounce
    • BEMAnimationTypeFlat
    • BEMAnimationTypeOneStroke
    • BEMAnimationTypeFade