@@ -24,6 +24,7 @@ @interface DNSCastroSegmentedControl()
2424@property (nonatomic ) NSLayoutConstraint *selectionLeftConstraint;
2525@property (nonatomic ) NSInteger initialConstraintConstant;
2626@property (nonatomic ) BOOL touchesInProgress;
27+ @property (nonatomic ) NSInteger valueAtStartOfTouches;
2728
2829@end
2930
@@ -412,7 +413,9 @@ - (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex
412413{
413414 if (_selectedSegmentIndex != selectedSegmentIndex) {
414415 _selectedSegmentIndex = selectedSegmentIndex;
415- [self sendActionsForControlEvents: UIControlEventValueChanged];
416+ if (!self.touchesInProgress ) {
417+ [self sendActionsForControlEvents: UIControlEventValueChanged];
418+ }
416419 }
417420}
418421
@@ -450,6 +453,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
450453{
451454 [super touchesBegan: touches withEvent: event];
452455 self.touchesInProgress = YES ;
456+ self.valueAtStartOfTouches = self.selectedSegmentIndex ;
453457
454458 UITouch *touch = [touches anyObject ];
455459 self.initialTouchPoint = [touch locationInView: self ];
@@ -512,12 +516,22 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
512516- (void )touchesCancelled : (NSSet *)touches withEvent : (UIEvent *)event
513517{
514518 [super touchesCancelled: touches withEvent: event];
519+
520+ // Reset the selected segment index to what it was initially.
521+ self.selectedSegmentIndex = self.valueAtStartOfTouches ;
522+
515523 [self touchesEndedOrCancelled ];
516524}
517525
518526- (void )touchesEndedOrCancelled
519527{
520528 self.touchesInProgress = NO ;
529+
530+ // If the value has changed, send that action.
531+ if (self.valueAtStartOfTouches != self.selectedSegmentIndex ) {
532+ [self sendActionsForControlEvents: UIControlEventValueChanged];
533+ }
534+
521535 [UIView animateWithDuration: AnimationDuration
522536 delay: 0
523537 options: UIViewAnimationOptionCurveEaseIn
0 commit comments