Skip to content

Commit 4bbabc5

Browse files
authored
Merge pull request #5 from TimOliver/reversible
Added Reversible Mode
2 parents 1eaa968 + 3dd234f commit 4bbabc5

9 files changed

Lines changed: 541 additions & 239 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
x.y.z Release Notes (yyyy-MM-dd)
22
=============================================================
33

4-
1.0.0 Release Notes (yyyy-MM-dd)
4+
1.0.0 Release Notes (2019-09-23)
55
=============================================================
66

77
### Enhancements
88

99
* Added dark mode support for iOS 13 and up.
10+
* Added optional 'reversible' mode for specific segments.
1011

1112
0.0.1 Release Notes (2019-09-15)
1213
=============================================================

Design/Arrowhead.pcvd

1.52 KB
Binary file not shown.

TOSegmentedControl/Private/TOSegmentedControlItem.h renamed to TOSegmentedControl/Private/TOSegmentedControlSegment.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ NS_ASSUME_NONNULL_BEGIN
3636
information and state for a single item in the
3737
segmented control.
3838
*/
39-
@interface TOSegmentedControlItem : NSObject
39+
@interface TOSegmentedControlSegment : NSObject
4040

4141
/** When item is a label, the text to display */
42-
@property (nonatomic, copy) NSString *title;
42+
@property (nonatomic, copy, nullable) NSString *title;
4343

4444
/** When item is an image, the image to display */
45-
@property (nonatomic, strong) UIImage *image;
45+
@property (nonatomic, strong, nullable) UIImage *image;
4646

4747
/** Whether the item can be tapped to toggle direction */
4848
@property (nonatomic, assign) BOOL isReversible;
@@ -53,6 +53,9 @@ NS_ASSUME_NONNULL_BEGIN
5353
/** Whether this item is enabled or disabled. */
5454
@property (nonatomic, assign) BOOL isDisabled;
5555

56+
/** Whether the item is selected or not. */
57+
@property (nonatomic, assign) BOOL isSelected;
58+
5659
/** The view (either image or label) for this item */
5760
@property (nonatomic, readonly) UIView *itemView;
5861

@@ -62,9 +65,12 @@ NS_ASSUME_NONNULL_BEGIN
6265
/** If the item is an image, the subsequent image view (nil if a string) */
6366
@property (nonatomic, nullable, readonly) UIImageView *imageView;
6467

68+
/** If the item is reversible, the subsequent arrow image view. */
69+
@property (nonatomic, nullable, readonly) UIView *arrowView;
70+
6571
// Create an array of objects given an array of strings and images
66-
+ (NSArray *)itemsWithObjects:(NSArray *)objects
67-
forSegmentedControl:(TOSegmentedControl *)segmentedControl;;
72+
+ (NSArray *)segmentsWithObjects:(NSArray *)objects
73+
forSegmentedControl:(TOSegmentedControl *)segmentedControl;;
6874

6975
// Create a non-reversible item from this class
7076
- (nullable instancetype)initWithObject:(id)object
@@ -88,6 +94,9 @@ NS_ASSUME_NONNULL_BEGIN
8894
// Re-synchronize the item view when the segmented control style changes
8995
- (void)refreshItemView;
9096

97+
// Rotates the arrow image view to 180 degrees and back again
98+
- (void)setArrowImageReversed:(BOOL)reversed;
99+
91100
@end
92101

93102
NS_ASSUME_NONNULL_END

TOSegmentedControl/Private/TOSegmentedControlItem.m renamed to TOSegmentedControl/Private/TOSegmentedControlSegment.m

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
2121
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222

23-
#import "TOSegmentedControlItem.h"
23+
#import "TOSegmentedControlSegment.h"
2424
#import "TOSegmentedControl.h"
2525
#import <UIKit/UIKit.h>
2626

@@ -35,20 +35,23 @@ @interface TOSegmentedControl ()
3535
// -------------------------------------------------
3636
// Private Interface
3737

38-
@interface TOSegmentedControlItem ()
38+
@interface TOSegmentedControlSegment ()
3939

4040
// Weak reference to our parent segmented control
4141
@property (nonatomic, weak) TOSegmentedControl *segmentedControl;
4242

4343
// Read-write access to the item view
4444
@property (nonatomic, strong, readwrite) UIView *itemView;
4545

46+
// When made reversible, the container for arrow image view to show
47+
@property (nonatomic, strong, readwrite) UIView *arrowView;
48+
4649
// When made reversible, the arrow image view to show
4750
@property (nonatomic, strong) UIImageView *arrowImageView;
4851

4952
@end
5053

51-
@implementation TOSegmentedControlItem
54+
@implementation TOSegmentedControlSegment
5255

5356
#pragma mark - Object Lifecyle -
5457

@@ -132,20 +135,20 @@ - (void)dealloc
132135

133136
#pragma mark - Comnvenience Initializers -
134137

135-
+ (NSArray *)itemsWithObjects:(NSArray *)objects forSegmentedControl:(nonnull TOSegmentedControl *)segmentedControl
138+
+ (NSArray *)segmentsWithObjects:(NSArray *)objects forSegmentedControl:(nonnull TOSegmentedControl *)segmentedControl
136139
{
137140
NSMutableArray *array = [NSMutableArray array];
138141

139142
// Create an object for each item in the array.
140143
// Skip anything that isn't an image or a label
141144
for (id object in objects) {
142-
TOSegmentedControlItem *item = nil;
145+
TOSegmentedControlSegment *item = nil;
143146
if ([object isKindOfClass:NSString.class]) {
144-
item = [[TOSegmentedControlItem alloc] initWithTitle:object
147+
item = [[TOSegmentedControlSegment alloc] initWithTitle:object
145148
forSegmentedControl:segmentedControl];
146149
}
147150
else if ([object isKindOfClass:UIImage.class]) {
148-
item = [[TOSegmentedControlItem alloc] initWithImage:object
151+
item = [[TOSegmentedControlSegment alloc] initWithImage:object
149152
forSegmentedControl:segmentedControl];
150153
}
151154

@@ -170,36 +173,30 @@ - (void)commonInit
170173

171174
- (void)refreshReversibleView
172175
{
173-
// Whether it's in memory or not, set the tint color
174-
self.arrowImageView.tintColor = self.segmentedControl.itemColor;
175-
176176
// If we're no longer (Or never were) reversible,
177177
// hide and exit out
178178
if (!self.isReversible) {
179-
self.arrowImageView.hidden = YES;
179+
self.arrowView.hidden = YES;
180180
return;
181181
}
182182

183183
// Create the arrow view if we haven't done so yet
184-
if (self.arrowImageView == nil) {
184+
if (self.arrowView == nil && self.arrowImageView == nil) {
185185
UIImage *arrow = self.segmentedControl.arrowImage;
186+
self.arrowView = [[UIView alloc] initWithFrame:(CGRect){CGPointZero, arrow.size}];
187+
self.arrowView.alpha = 0.0f;
188+
[self.segmentedControl.trackView addSubview:self.arrowView];
189+
186190
self.arrowImageView = [[UIImageView alloc] initWithImage:arrow];
191+
self.arrowImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
192+
[self.arrowView addSubview:self.arrowImageView];
187193
}
188-
189-
// Add the arrow to the item view
190-
self.itemView.clipsToBounds = NO;
191-
[self.itemView addSubview:self.arrowImageView];
192-
193-
// Line up the item view vertically centered next to the item view
194-
self.arrowImageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |
195-
UIViewAutoresizingFlexibleBottomMargin |
196-
UIViewAutoresizingFlexibleLeftMargin;
197-
198-
CGRect itemFrame = self.itemView.frame;
199-
CGRect frame = self.arrowImageView.frame;
200-
frame.origin.x = CGRectGetMaxX(itemFrame) + 2.0f;
201-
frame.origin.y = (CGRectGetHeight(itemFrame) - CGRectGetHeight(frame)) * 0.5f;
202-
self.arrowImageView.frame = frame;
194+
195+
// Perform these updates, but never animate them
196+
[UIView performWithoutAnimation:^{
197+
// Set the tint color
198+
self.arrowImageView.tintColor = self.segmentedControl.itemColor;
199+
}];
203200
}
204201

205202
#pragma mark - View Management -
@@ -321,4 +318,14 @@ - (UIImageView *)imageView
321318
return nil;
322319
}
323320

321+
- (void)setArrowImageReversed:(BOOL)reversed
322+
{
323+
if (reversed) {
324+
self.arrowImageView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI);
325+
return;
326+
}
327+
328+
self.arrowImageView.transform = CGAffineTransformIdentity;
329+
}
330+
324331
@end

0 commit comments

Comments
 (0)