Created
February 1, 2017 20:00
-
-
Save DonMag/99b1cacb1ca8d3a173063db4aedfa1b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // RoundedUIView.h | |
| // TmpRoundable | |
| // | |
| // Created by Don Mag on 2/1/17. | |
| // Copyright © 2017 DonMag. All rights reserved. | |
| // | |
| #import <UIKit/UIKit.h> | |
| IB_DESIGNABLE | |
| @interface RoundedUIView : UIView | |
| @property (nonatomic, assign) IBInspectable CGFloat TopLeftCornerRadius; | |
| @property (nonatomic, assign) IBInspectable CGFloat TopRightCornerRadius; | |
| @property (nonatomic, assign) IBInspectable CGFloat BottomLeftCornerRadius; | |
| @property (nonatomic, assign) IBInspectable CGFloat BottomRightCornerRadius; | |
| @property (nonatomic, assign) IBInspectable BOOL AllCornersTheSame; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // RoundedUIView.m | |
| // TmpRoundable | |
| // | |
| // Created by Don Mag on 2/1/17. | |
| // Copyright © 2017 DonMag. All rights reserved. | |
| // | |
| #import "RoundedUIView.h" | |
| @interface RoundedUIView() | |
| @property (nonatomic, strong) CAShapeLayer *rLayer; | |
| @end | |
| @implementation RoundedUIView | |
| - (void)setupDefaults { | |
| if (self.rLayer == nil) { | |
| self.rLayer = [CAShapeLayer layer]; | |
| self.layer.mask = self.rLayer; | |
| } | |
| self.TopLeftCornerRadius = self.TopRightCornerRadius = self.BottomRightCornerRadius = self.BottomLeftCornerRadius = 0.0f; | |
| self.AllCornersTheSame = YES; | |
| } | |
| - (instancetype)initWithFrame:(CGRect)frame | |
| { | |
| self = [super initWithFrame:frame]; | |
| if (self) { | |
| [self setupDefaults]; | |
| } | |
| return self; | |
| } | |
| - (instancetype)initWithCoder:(NSCoder *)coder | |
| { | |
| self = [super initWithCoder:coder]; | |
| if (self) { | |
| [self setupDefaults]; | |
| } | |
| return self; | |
| } | |
| - (void)layoutSubviews { | |
| [super layoutSubviews]; | |
| [self updateLayerProperties]; | |
| } | |
| - (void)updateLayerProperties { | |
| UIBezierPath *pth = [[UIBezierPath alloc] init]; | |
| CGPoint p = CGPointZero; | |
| CGFloat tl = _TopLeftCornerRadius, tr = _TopRightCornerRadius, br = _BottomRightCornerRadius, bl = _BottomLeftCornerRadius; | |
| CGFloat w = self.bounds.size.width, h = self.bounds.size.height; | |
| if (_AllCornersTheSame) { | |
| tl = tr = br = bl = _TopLeftCornerRadius; | |
| } | |
| p.x = w - tr; | |
| [pth moveToPoint:p]; | |
| p.y = tr; | |
| [pth addArcWithCenter:p radius:tr startAngle:3 * M_PI_2 endAngle:0 clockwise:YES]; | |
| p.x = w; | |
| p.y = h - br; | |
| [pth addLineToPoint:p]; | |
| p.x = w - br; | |
| [pth addArcWithCenter:p radius:br startAngle:0 endAngle:M_PI_2 clockwise:YES]; | |
| p.x = bl; | |
| p.y = h; | |
| [pth addLineToPoint:p]; | |
| p.y = h - bl; | |
| [pth addArcWithCenter:p radius:bl startAngle:M_PI_2 endAngle:2 * M_PI_2 clockwise:YES]; | |
| p.x = 0; | |
| p.y = tl; | |
| [pth addLineToPoint:p]; | |
| p.x = tl; | |
| [pth addArcWithCenter:p radius:tl startAngle:2 * M_PI_2 endAngle:3 * M_PI_2 clockwise:YES]; | |
| [pth closePath]; | |
| self.rLayer.path = pth.CGPath; | |
| } | |
| - (void)setTopLeftCornerRadius:(CGFloat)TopLeftCornerRadius { | |
| if (TopLeftCornerRadius != _TopLeftCornerRadius) { | |
| _TopLeftCornerRadius = TopLeftCornerRadius; | |
| [self updateLayerProperties]; | |
| } | |
| } | |
| - (void)setTopRightCornerRadius:(CGFloat)TopRightCornerRadius { | |
| if (TopRightCornerRadius != _TopRightCornerRadius) { | |
| _TopRightCornerRadius = TopRightCornerRadius; | |
| [self updateLayerProperties]; | |
| } | |
| } | |
| - (void)setBottomRightCornerRadius:(CGFloat)BottomRightCornerRadius { | |
| if (BottomRightCornerRadius != _BottomRightCornerRadius) { | |
| _BottomRightCornerRadius = BottomRightCornerRadius; | |
| [self updateLayerProperties]; | |
| } | |
| } | |
| - (void)setBottomLeftCornerRadius:(CGFloat)BottomLeftCornerRadius { | |
| if (BottomLeftCornerRadius != _BottomLeftCornerRadius) { | |
| _BottomLeftCornerRadius = BottomLeftCornerRadius; | |
| [self updateLayerProperties]; | |
| } | |
| } | |
| - (void)setAllCornersTheSame:(BOOL)AllCornersTheSame { | |
| if (AllCornersTheSame != _AllCornersTheSame) { | |
| _AllCornersTheSame = AllCornersTheSame; | |
| [self updateLayerProperties]; | |
| } | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment