1818
1919@interface DNSCastroSegmentedControl ()
2020@property (nonatomic ) UIView *selectionView;
21+ @property (nonatomic ) UIView *selectionBackgroundView;
2122@property (nonatomic ) NSArray *sectionViews;
2223@property (nonatomic ) CGPoint initialTouchPoint;
2324@property (nonatomic ) NSLayoutConstraint *selectionLeftConstraint;
@@ -41,6 +42,7 @@ - (void)layoutSubviews
4142 [self setupKVO ];
4243 [self setupSectionViews ];
4344 [self setupSelectionView ];
45+ [self setupSelectionBackgroundView ];
4446 [self roundAllTheThings ];
4547 [self setSelectedSegmentIndex: self .selectedSegmentIndex animated: NO ];
4648 }
@@ -125,11 +127,52 @@ - (void)setupSelectionView
125127 [self addConstraint: self .selectionLeftConstraint];
126128}
127129
130+ - (void )setupSelectionBackgroundView
131+ {
132+ self.selectionBackgroundView = [[UIView alloc ] init ];
133+ self.selectionBackgroundView .translatesAutoresizingMaskIntoConstraints = NO ;
134+
135+ if (self.selectionBackgroundColor ) {
136+ self.selectionBackgroundView .backgroundColor = self.selectionBackgroundColor ;
137+ } else {
138+ self.selectionBackgroundView .backgroundColor = [UIColor clearColor ];
139+ }
140+
141+ // Insert below the labels
142+ [self insertSubview: self .selectionBackgroundView belowSubview: [self .sectionViews firstObject ]];
143+
144+ // Pin in all directions to the chiclet.
145+ [self pinView: self .selectionBackgroundView
146+ toOtherView: self .selectionView
147+ attribute: NSLayoutAttributeRight];
148+ [self pinView: self .selectionBackgroundView
149+ toOtherView: self .selectionView
150+ attribute: NSLayoutAttributeLeft];
151+ [self pinView: self .selectionBackgroundView
152+ toOtherView: self .selectionView
153+ attribute: NSLayoutAttributeTop];
154+ [self pinView: self .selectionBackgroundView
155+ toOtherView: self .selectionView
156+ attribute: NSLayoutAttributeBottom];
157+ }
158+
128159- (void )roundAllTheThings
129160{
130161 CGFloat cornerRadius = (CGRectGetHeight (self.frame ) / 2 );
131162 self.layer .cornerRadius = cornerRadius;
132163 self.selectionView .layer .cornerRadius = cornerRadius - SelectionViewPadding;
164+ self.selectionBackgroundView .layer .cornerRadius = cornerRadius - SelectionViewPadding;
165+ }
166+
167+ - (void )pinView : (UIView *)view1 toOtherView : (UIView *)view2 attribute : (NSLayoutAttribute )attribute
168+ {
169+ [self addConstraint: [NSLayoutConstraint constraintWithItem: view1
170+ attribute: attribute
171+ relatedBy: NSLayoutRelationEqual
172+ toItem: view2
173+ attribute: attribute
174+ multiplier: 1
175+ constant: 0 ]];
133176}
134177
135178- (void )pinViewToWidth : (UIView *)view withPadding : (CGFloat)padding
@@ -383,6 +426,15 @@ - (void)setSelectionViewColor:(UIColor *)selectionViewColor
383426 }
384427}
385428
429+ - (void )setSelectionBackgroundColor : (UIColor *)selectionBackgroundColor
430+ {
431+ _selectionBackgroundColor = selectionBackgroundColor;
432+
433+ if (self.selectionBackgroundView ) {
434+ self.selectionBackgroundView .backgroundColor = selectionBackgroundColor;
435+ }
436+ }
437+
386438- (void )setSelectedSegmentIndex : (NSInteger )selectedSegmentIndex
387439{
388440 if (_selectedSegmentIndex != selectedSegmentIndex) {
@@ -412,6 +464,7 @@ - (void)animateSelectionViewUpIfNeeded:(BOOL)shorten
412464 options: UIViewAnimationOptionCurveEaseOut
413465 animations: ^{
414466 self.selectionView .transform = CGAffineTransformMakeScale (scaleXPercentage, scaleYPercentage);
467+ self.selectionBackgroundView .transform = CGAffineTransformMakeScale (scaleXPercentage, scaleYPercentage);
415468 [self layoutIfNeeded ];
416469 }
417470 completion: nil ];
@@ -497,6 +550,7 @@ - (void)touchesEndedOrCancelled
497550 options: UIViewAnimationOptionCurveEaseIn
498551 animations: ^{
499552 self.selectionView .transform = CGAffineTransformIdentity;
553+ self.selectionBackgroundView .transform = CGAffineTransformIdentity;
500554 // Calls LayoutIfNeeded - no need to call it seperately.
501555 [self snapToCurrentSection: NO ];
502556 }
0 commit comments