Skip to content

Instantly share code, notes, and snippets.

@cocoaNib
Last active April 18, 2021 23:24
Show Gist options
  • Save cocoaNib/4f8f113aa54a12e3cb1f to your computer and use it in GitHub Desktop.
Save cocoaNib/4f8f113aa54a12e3cb1f to your computer and use it in GitHub Desktop.
UISegmentedControl with action for tap on selected segment
// Code for ViewController
import UIKit
class UIViewController {
@IBOutlet weak var segmentedControl: SegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
segmentedControl.addTarget(self, action: "tapSegment:", forControlEvents:.TouchDown)
}
func tapSegment(sender: UISegmentedControl) {
// Action for touchDown-Event on an already selected segment
}
}
// Code for SegmentedControl
import UIKit
class SegmentedControl: UISegmentedControl {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let currentIndex = selectedSegmentIndex
super.touchesBegan(touches, withEvent: event)
if currentIndex == selectedSegmentIndex {
sendActionsForControlEvents(UIControlEvents.TouchDown)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment