Skip to content
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ pod 'Switch'
Using Switch is quite simple :
```objective-c
UIImage* image = [UIImage imageNamed:@"switch.png"];
Switch* mySwitch = [Switch switchWithImage:image visibleWidth:200];
Switch* mySwitch = [Switch switchWithImage:image visibleWidth:200 visibleWidthViewImageRatio:1];
[mySwitch addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mySwitch];
```

* Switch uses the image and visible width combination to toggle between states.
* Use visibleWidthViewImageRatio = 1 if image visible width corresponds to the expected view width in pixels. Otherwise pass ratio calculated as [expected view width] / visibleWidth
* You can provide cornerRadius of your choice to make it appear roundedCorner style or any other.

## What's the catch ?
Expand Down
9 changes: 7 additions & 2 deletions Switch/Switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ add -fobjc-arc flag for Switch.m file in Build Phases -> Compile Sources.
//Width for switch frame, should always be less than the image's width
@property(nonatomic,assign)CGFloat visibleWidth;

@property(nonatomic,assign)CGFloat visibleWidthViewImageRatio;

//Origin helps in positioning the switch, as width,height depend on image
@property(nonatomic,assign)CGPoint origin;

Expand All @@ -46,8 +48,11 @@ add -fobjc-arc flag for Switch.m file in Build Phases -> Compile Sources.
//ON-OFF toggle boolean
@property(nonatomic,assign)BOOL on;

//Class Helper to instantiate Switch with image & expected visibleWidth
//Class Helper to instantiate Switch with image & expected visibleWidth of image.
// visibleWidthViewImageRatio is a ratio between expected switch view width
// and visible image width
+(Switch*)switchWithImage:(UIImage*)switchImage
visibleWidth:(CGFloat)visibleWidth;
visibleWidth:(CGFloat)visibleWidth
visibleWidthViewImageRatio:(CGFloat)visibleWidthViewImageRatio;

@end
37 changes: 22 additions & 15 deletions Switch/Switch.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

#import "Switch.h"
#define Scale [UIScreen mainScreen].scale

@interface Switch()
{
Expand All @@ -37,20 +36,22 @@ @implementation Switch

+(Switch*)switchWithImage:(UIImage*)switchImage
visibleWidth:(CGFloat)visibleWidth
visibleWidthViewImageRatio:(CGFloat)visibleWidthViewImageRatio
{
return [[self alloc] initSwitchWithImage:switchImage visibleWidth:visibleWidth];
return [[self alloc] initSwitchWithImage:switchImage visibleWidth:visibleWidth visibleWidthViewImageRatio:visibleWidthViewImageRatio];
}

-(id)initSwitchWithImage:(UIImage*)switchImage visibleWidth:(CGFloat)visibleWidth
-(id)initSwitchWithImage:(UIImage*)switchImage visibleWidth:(CGFloat)visibleWidth visibleWidthViewImageRatio:(CGFloat)visibleWidthViewImageRatio
{
if(self = [super init])
{
_image = switchImage;
_visibleWidth = visibleWidth;
_visibleWidthViewImageRatio = visibleWidthViewImageRatio;
_origin = CGPointZero;

self.frame = CGRectMake(_origin.x, _origin.y,
_visibleWidth, _image.size.height/Scale);
_visibleWidth, self.visibleWidthViewImageRatio * _image.size.height);
self.clipsToBounds = YES;

imgVw = [[UIImageView alloc] initWithFrame:CGRectZero];
Expand All @@ -72,34 +73,40 @@ -(id)initSwitchWithImage:(UIImage*)switchImage visibleWidth:(CGFloat)visibleWidt
return self;
}

- (void)layoutSubviews
{
[super layoutSubviews];

self.frame = CGRectMake(self.origin.x, self.origin.y, self.visibleWidthViewImageRatio * self.visibleWidth, self.visibleWidthViewImageRatio * self.image.size.height);

imgVw.bounds = CGRectMake(0.f, 0.f,
self.visibleWidthViewImageRatio * self.image.size.width,
self.visibleWidthViewImageRatio * self.image.size.height);
imgVw.center = CGPointMake(self.on ? 0.5f * CGRectGetWidth(imgVw.bounds) : 0.f,
0.5f * CGRectGetHeight(self.bounds));
}

#pragma mark
#pragma mark<Property Setters>
#pragma mark

-(void)setVisibleWidth:(CGFloat)visibleWidth
{
_visibleWidth = visibleWidth;

self.frame = CGRectMake(_origin.x, _origin.y, _visibleWidth, self.frame.size.height);
imgVw.frame = CGRectMake((_on ? 0 : -(_image.size.width/Scale - _visibleWidth)), 0,
_image.size.width/Scale, _image.size.height/Scale);
[self setNeedsLayout];
}

-(void)setOrigin:(CGPoint)origin
{
_origin = origin;

self.frame = CGRectMake(_origin.x, _origin.y, _visibleWidth, self.frame.size.height);
[self setNeedsLayout];
}

-(void)setImage:(UIImage*)image
{
_image = image;

self.frame = CGRectMake(_origin.x, _origin.y, _visibleWidth, _image.size.height/Scale);
imgVw.frame = CGRectMake((_on ? 0 : -(_image.size.width/Scale - _visibleWidth)), 0,
_image.size.width/Scale, _image.size.height/Scale);
imgVw.image = _image;
[self setNeedsLayout];
}

#pragma mark
Expand All @@ -116,7 +123,7 @@ -(void)setOn:(BOOL)on
animations:^
{
self.userInteractionEnabled = NO;
imgVw.frame = CGRectMake((_on ? 0 : -(imgVw.frame.size.width-_visibleWidth)), 0,
imgVw.frame = CGRectMake((_on ? 0 : -(imgVw.frame.size.width-_visibleWidth * self.visibleWidthViewImageRatio)), 0,
imgVw.frame.size.width,
imgVw.frame.size.height);
}
Expand Down