Last active
April 18, 2021 23:24
-
-
Save cocoaNib/4f8f113aa54a12e3cb1f to your computer and use it in GitHub Desktop.
UISegmentedControl with action for tap on selected segment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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