From 5933664300091d37ab2f95b41015bbd7e91e10f6 Mon Sep 17 00:00:00 2001 From: pavel yurchenko Date: Fri, 15 Jan 2016 13:58:25 +0300 Subject: [PATCH 1/3] Fixed issue with incorrect layout for iPhone 6 Plus --- Switch/Switch.h | 9 +++++++-- Switch/Switch.m | 37 ++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/Switch/Switch.h b/Switch/Switch.h index c12aa2b..33c0324 100644 --- a/Switch/Switch.h +++ b/Switch/Switch.h @@ -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; @@ -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 diff --git a/Switch/Switch.m b/Switch/Switch.m index bd6add1..343ca7f 100644 --- a/Switch/Switch.m +++ b/Switch/Switch.m @@ -23,7 +23,6 @@ */ #import "Switch.h" -#define Scale [UIScreen mainScreen].scale @interface Switch() { @@ -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]; @@ -72,6 +73,19 @@ -(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 #pragma mark @@ -79,27 +93,20 @@ -(id)initSwitchWithImage:(UIImage*)switchImage visibleWidth:(CGFloat)visibleWidt -(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 @@ -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); } From 9b60e344fd0e1dc4b5faded28e61036cc07e78ad Mon Sep 17 00:00:00 2001 From: pavel yurchenko Date: Fri, 15 Jan 2016 14:04:32 +0300 Subject: [PATCH 2/3] ReadMe changes --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ef986df..7f87c09 100755 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ 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]; ``` From c9eccde25ed83828111ee6e92505af234cf7e95f Mon Sep 17 00:00:00 2001 From: pavel yurchenko Date: Fri, 15 Jan 2016 14:07:23 +0300 Subject: [PATCH 3/3] ReadMe changes --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7f87c09..311f01c 100755 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Switch* mySwitch = [Switch switchWithImage:image visibleWidth:200 visibleWidthVi ``` * 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 ?