Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Demo/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Demo/1@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Demo/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Demo/2@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Demo/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Demo/3@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 37 additions & 3 deletions Demo/Classes/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ - (void)viewDidLoad {

[self.view addSubview:redSC];

redSC.center = CGPointMake(160, 170);
redSC.center = CGPointMake(160, 150);


// 3rd CONTROL
Expand All @@ -50,7 +50,7 @@ - (void)viewDidLoad {

[self.view addSubview:grayRC];

grayRC.center = CGPointMake(160, 270);
grayRC.center = CGPointMake(160, 230);


// 4th CONTROL
Expand All @@ -68,7 +68,41 @@ - (void)viewDidLoad {

[self.view addSubview:yellowRC];

yellowRC.center = CGPointMake(160, 370);
yellowRC.center = CGPointMake(160, 310);


// 4th CONTROL
SVSegmentedControl *imageRC = [[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:@"Some", @"Section", @"Images", nil]];
[imageRC setSectionImages:@[[UIImage imageNamed:@"1"], [UIImage imageNamed:@"2"], [UIImage imageNamed:@"3"], [UIImage imageNamed:@"3"]]];
[imageRC addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
imageRC.crossFadeLabelsOnDrag = YES;
imageRC.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
imageRC.height = 40;
[imageRC setSelectedSegmentIndex:0 animated:NO];
imageRC.thumb.tintColor = [UIColor colorWithRed:0.100 green:0.400 blue:0.800 alpha:1.000];
imageRC.thumb.textColor = [UIColor whiteColor];
imageRC.thumb.textColor = [UIColor whiteColor];
imageRC.thumb.textShadowColor = [UIColor colorWithWhite:0 alpha:0.5];
imageRC.thumb.textShadowOffset = CGSizeMake(0, 1);

[self.view addSubview:imageRC];

imageRC.center = CGPointMake(160, 390);


// 5th CONTROL
SVSegmentedControl *iOS7FlatRC = [[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:@"Can", @"Look", @"Flat", nil] stylePreset:SVSegmentedControlStylePresetFlat];
[iOS7FlatRC addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
iOS7FlatRC.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
iOS7FlatRC.height = 40;
[iOS7FlatRC setSelectedSegmentIndex:1 animated:NO];
iOS7FlatRC.thumb.tintColor = [UIColor colorWithRed:1.000 green:0.300 blue:0.100 alpha:1.000];
iOS7FlatRC.thumb.textColor = [UIColor whiteColor];
iOS7FlatRC.thumb.textColor = [UIColor whiteColor];

[self.view addSubview:iOS7FlatRC];

iOS7FlatRC.center = CGPointMake(160, 470);
}


Expand Down
24 changes: 24 additions & 0 deletions Demo/SVSegmentedControl.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
2899E5220DE3E06400AC0155 /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2899E5210DE3E06400AC0155 /* ViewController.xib */; };
28AD733F0D9D9553002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD733E0D9D9553002E5188 /* MainWindow.xib */; };
28D7ACF80DDB3853001CB0EB /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 28D7ACF70DDB3853001CB0EB /* ViewController.m */; };
651F8D091940E22A00345FC0 /* 1.png in Resources */ = {isa = PBXBuildFile; fileRef = 651F8D031940E22A00345FC0 /* 1.png */; };
651F8D0A1940E22A00345FC0 /* 1@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 651F8D041940E22A00345FC0 /* 1@2x.png */; };
651F8D0B1940E22A00345FC0 /* 2.png in Resources */ = {isa = PBXBuildFile; fileRef = 651F8D051940E22A00345FC0 /* 2.png */; };
651F8D0C1940E22A00345FC0 /* 2@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 651F8D061940E22A00345FC0 /* 2@2x.png */; };
651F8D0D1940E22A00345FC0 /* 3.png in Resources */ = {isa = PBXBuildFile; fileRef = 651F8D071940E22A00345FC0 /* 3.png */; };
651F8D0E1940E22A00345FC0 /* 3@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 651F8D081940E22A00345FC0 /* 3@2x.png */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -44,6 +50,12 @@
28D7ACF70DDB3853001CB0EB /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
32CA4F630368D1EE00C91783 /* SVSegmentedControl_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SVSegmentedControl_Prefix.pch; sourceTree = "<group>"; };
651F8D031940E22A00345FC0 /* 1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = 1.png; sourceTree = "<group>"; };
651F8D041940E22A00345FC0 /* 1@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "1@2x.png"; sourceTree = "<group>"; };
651F8D051940E22A00345FC0 /* 2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = 2.png; sourceTree = "<group>"; };
651F8D061940E22A00345FC0 /* 2@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "2@2x.png"; sourceTree = "<group>"; };
651F8D071940E22A00345FC0 /* 3.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = 3.png; sourceTree = "<group>"; };
651F8D081940E22A00345FC0 /* 3@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "3@2x.png"; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* SVSegmentedControl-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "SVSegmentedControl-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -124,6 +136,12 @@
children = (
28AD733E0D9D9553002E5188 /* MainWindow.xib */,
8D1107310486CEB800E47090 /* SVSegmentedControl-Info.plist */,
651F8D031940E22A00345FC0 /* 1.png */,
651F8D041940E22A00345FC0 /* 1@2x.png */,
651F8D051940E22A00345FC0 /* 2.png */,
651F8D061940E22A00345FC0 /* 2@2x.png */,
651F8D071940E22A00345FC0 /* 3.png */,
651F8D081940E22A00345FC0 /* 3@2x.png */,
);
name = Resources;
sourceTree = "<group>";
Expand Down Expand Up @@ -191,9 +209,15 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
651F8D0A1940E22A00345FC0 /* 1@2x.png in Resources */,
651F8D0E1940E22A00345FC0 /* 3@2x.png in Resources */,
651F8D0B1940E22A00345FC0 /* 2.png in Resources */,
651F8D0D1940E22A00345FC0 /* 3.png in Resources */,
651F8D091940E22A00345FC0 /* 1.png in Resources */,
28AD733F0D9D9553002E5188 /* MainWindow.xib in Resources */,
2899E5220DE3E06400AC0155 /* ViewController.xib in Resources */,
224B1756145F25D600BD2DDB /* README.md in Resources */,
651F8D0C1940E22A00345FC0 /* 2@2x.png in Resources */,
22A748901630AD4E004893A8 /* Default-568h@2x.png in Resources */,
223F841D16BC891900C3C219 /* SVSegmentedControl.podspec in Resources */,
);
Expand Down
162 changes: 162 additions & 0 deletions SVSegmentedControl/SVSegmentedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,200 @@
#import "SVSegmentedThumb.h"
#import <AvailabilityMacros.h>

/**
* Default initial styles to enable or disable multiple visual attributes at once.
*/
typedef NS_ENUM(NSInteger, SVSegmentedControlStylePreset)
{
/**
* The default style used when nothing is set. Shadows and gradient are intact.
*/
SVSegmentedControlStylePresetDefault,
/**
* A flat style to match iOS 7+ style layouts. All shadows and gradient disabled.
*/
SVSegmentedControlStylePresetFlat,
};

@protocol SVSegmentedControlDelegate;

/**
* SVSegmentedControl is a customizable UIControl class that mimics UISegmentedControl but that looks like an UISwitch.
* In its simplest form, this is how you create an SVSegmentedControl instance:
* @code
segmentedControl = [[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:@"Section 1", @"Section 2", nil]];
segmentedControl.changeHandler = ^(NSUInteger newIndex) {
// respond to index change
};

[self.view addSubview:segmentedControl];
* @endcode
* You can position it using either its frame or center property:
*/
@interface SVSegmentedControl : UIControl

/**
* A block called whenever there is a change to the switch.
* If you haven't fallen in love with blocks yet, you can still use the classic UIControl method:
* @code
[mySegmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
* @endcode
*
* Providing an action method ending with a semicolon, the sender object is therefore made accessible:
* @code
- (void)segmentedControlChangedValue:(SVSegmentedControl*)segmentedControl {
NSLog(@"segmentedControl did select index %i", segmentedControl.selectedIndex);
}
* @endcode
*/
@property (nonatomic, copy) void (^changeHandler)(NSUInteger newIndex); // you can also use addTarget:action:forControlEvents:

/**
* The titles for each section of the switch. These are laid out in the order specicied in the array.
*/
@property (nonatomic, copy) NSArray *sectionTitles;

/**
* Images placed to the left of section titles. Images are assigned to titles with corresponding indices.
* If not enough images are provided, the remaining section(s) will be displayed normally.
* If too many images are provided, the remaining images are not displayed.
*/
@property (nonatomic, copy) NSArray *sectionImages;

/**
* The thumb that moves to the selected section. Access this property to change its appearance.
*/
@property (nonatomic, strong, readonly) SVSegmentedThumb *thumb;

/**
* The currently selected index of the switch. The default value is 0.
*/
@property (nonatomic, readonly) NSUInteger selectedSegmentIndex; // default is 0

/**
* Whether or not to animated the change to initial selection. The default value is NO.
* @remarks This doesn't seem to be used anywhere.
*/
@property (nonatomic, readwrite) BOOL animateToInitialSelection; // default is NO

/**
* Whether or not to change the title on the thumb, or slide the thumb over the titles.
* If set to NO (the default) the thumb moves while the old and new titles stay lined up with the titles
* in the background, sliding on and off the thumb.
* If set to YES, as the thumb moves, the title stays in place and fades between the two sections.
*/
@property (nonatomic, readwrite) BOOL crossFadeLabelsOnDrag; // default is NO

/**
* Whether the switch must be changed with a pan gesture to make the control difficult to accidentally change.
* The default is NO.
*/
@property (nonatomic, readwrite) BOOL mustSlideToChange; // default is NO - To make the control difficult to accidentally change, force the user to slide it

/**
* The fractional amount by which the thumb must overlap a destination segment to cause a snap to that segment.
* The default value is 0.66.
*/
@property (nonatomic, readwrite) CGFloat minimumOverlapToChange; // default is 0.66 - Only snap to a new segment if the thumb overlaps it by this fraction

/**
* Margins to add to the touchable area of the control.
* The default is UIEdgeInsetsMake(0, 0, 0, 0).
*/
@property (nonatomic, readwrite) UIEdgeInsets touchTargetMargins; // default is UIEdgeInsetsMake(0, 0, 0, 0) - Enlarge touch target of control

/**
* The color used in the background of the switch.
* The default is [UIColor colorWithWhite:0.1 alpha:1].
*/
@property (nonatomic, strong) UIColor *backgroundTintColor; // default is [UIColor colorWithWhite:0.1 alpha:1]

/**
* The image used in the background of the switch.
* The default is nil.
*/
@property (nonatomic, strong) UIImage *backgroundImage; // default is nil

/**
* The height of the control. The default is 32.0.
*/
@property (nonatomic, readwrite) CGFloat height; // default is 32.0

/**
* The insets of the thumb from the boundries of the segment and the control.
* The default is UIEdgeInsetsMake(2, 2, 3, 2).
*/
@property (nonatomic, readwrite) UIEdgeInsets thumbEdgeInset; // default is UIEdgeInsetsMake(2, 2, 3, 2)

/**
* The insets from the edge of the control to the edges of the segment titles.
* The default is UIEdgeInsetsMake(0, 10, 0, 10).
*/
@property (nonatomic, readwrite) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsMake(0, 10, 0, 10)

/**
* The corner radius of the control. The default is 4.0.
*/
@property (nonatomic, readwrite) CGFloat cornerRadius; // default is 4.0

/**
* The font used for the titles. The default is [UIFont boldSystemFontOfSize:15].
*/
@property (nonatomic, strong) UIFont *font; // default is [UIFont boldSystemFontOfSize:15]

/**
* The color of the text in the segments that are not selected. The default is [UIColor grayColor].
*/
@property (nonatomic, strong) UIColor *textColor; // default is [UIColor grayColor];

/**
* The color of the shadow for the text in the segments that are not selected. The default is [UIColor blackColor].
*/
@property (nonatomic, strong) UIColor *textShadowColor; // default is [UIColor blackColor]

/**
* The offset of the shadow from the text in the segments that are not selected. The default is CGSizeMake(0, -1).
*/
@property (nonatomic, readwrite) CGSize textShadowOffset; // default is CGSizeMake(0, -1)

/**
* The color of the shadow inside the control along the edges. The default is [UIColor colorWithWhite:0 alpha:0.8].
*/
@property (nonatomic, strong) UIColor *innerShadowColor; // default is [UIColor colorWithWhite:0 alpha:0.8]

/**
* The preset style. Setting this will change all documented shadow colors, the thumb gradient,
* and remove the bottom gloss. The default value is SVSegmentedControlStylePresetDefault.
*/
@property (nonatomic, readwrite) SVSegmentedControlStylePreset stylePreset;

/**
* Make a new segmented control with the specified section titles.
* Initialized with SVSegmentedControlStylePresetDefault.
*
* @param titlesArray an array of section titles for the new control
*
* @return a new segemented control with the specified titles
*/
- (SVSegmentedControl*)initWithSectionTitles:(NSArray*)titlesArray;

/**
* Make a new segmented control with the specified section titles and style.
*
*
* @param titlesArray an array of section titles for the new control
* @param style the initial styling preset for the control
*
* @return a new segmented control with the specified titles and style preset
*/
- (SVSegmentedControl*)initWithSectionTitles:(NSArray*)titlesArray stylePreset:(SVSegmentedControlStylePreset)stylePreset;

/**
* Selects a specified index in the control. Either animates or jumps straight to the new state.
*
* @param index the new index to select
* @param animated whether to animate the change in selection
*/
- (void)setSelectedSegmentIndex:(NSUInteger)index animated:(BOOL)animated;

// deprecated
Expand Down
Loading