Skip to content

Commit

Permalink
Add Deselection State
Browse files Browse the repository at this point in the history
  • Loading branch information
alxrguz committed Oct 11, 2020
1 parent 31c2ec8 commit e120b4a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion ALRadioButtons.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "ALRadioButtons"
spec.version = "1.0.1"
spec.version = "1.1.0"
spec.summary = "RadioButtons for iOS. Inherited from UIControl, support 2 native styles, fully customizable."

spec.homepage = "https://github.com/alxrguz/ALRadioButtons"
Expand Down
Binary file modified Assets/ALRadioButtons.sketch
Binary file not shown.
Binary file modified Assets/socialPreview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pod 'ALRadioButtons'

#### Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate SnapKit into your project manually. Put `Source/ALRadioButtons` folder in your Xcode project.
If you prefer not to use either of the aforementioned dependency managers, you can integrate ALRadioButtons into your project manually. Put `Source/ALRadioButtons` folder in your Xcode project.



Expand All @@ -91,7 +91,10 @@ class MyViewController: UIViewController {
// ... Setup layout

radioGroup.selectedIndex = 0
radioGroup.addTarget(self, action: #selector(radioGroupSelected(_:)), for: .valueChanged)
radioGroup.addTarget(self, action: #selector(radioGroupSelected(_:)), for: .valueChanged)

// If the checkbox selection can be canceled, then set this property to true
radioGroup.allowDeselection = true
}

@objc private func radioGroupSelected(_ sender: ALRadioGroup) {
Expand Down
16 changes: 14 additions & 2 deletions Sources/ALRadioButtons/Views/ALRadioGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public final class ALRadioGroup: UIControl {
}
}

/**
If this parameter is set to true, then by tap on an already selected item, the selection will be canceled, and the `selectedIndex` will become `-1`. Default is false
*/
public var allowDeselection: Bool = false

/**
Title color if `ALRadioButton` is selected
*/
Expand Down Expand Up @@ -261,8 +266,15 @@ public final class ALRadioGroup: UIControl {
private extension ALRadioGroup {
@objc func radioItemTapped(_ sender: UITapGestureRecognizer) {
guard let radioItem = (sender.view as? ALRadioButton),
let index = items.firstIndex(of: radioItem),
selectedIndex != index else { return }
var index = items.firstIndex(of: radioItem) else { return }

if index == selectedIndex {
if allowDeselection {
index = -1
} else {
return
}
}

if hapticResponse { haptic.selectionChanged() }
selectedIndex = index
Expand Down

0 comments on commit e120b4a

Please sign in to comment.