Last active
April 1, 2016 11:59
-
-
Save KalpeshTalkar/ce83311390be7861359f2cad3a4efd96 to your computer and use it in GitHub Desktop.
KBorderedGradientView, a custom IBDesignable UIView
This file contains 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
// | |
// KBorderedGradientView.h | |
// | |
// Created by Kalpesh Talkar on 01/02/16. | |
// Copyright © 2016 Kalpesh. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
IB_DESIGNABLE | |
@interface KBorderedGradientView : UIView | |
- (void)setCornerRadius:(CGFloat)cornerRadius; | |
- (void)setBorderWidth:(CGFloat)borderWidth; | |
- (void)setBorderColor:(UIColor *)borderColor; | |
- (void)setStartColor:(UIColor *)startColor; | |
- (void)setEndColor:(UIColor *)endColor; | |
- (void)setGradientColorWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor horizontal:(BOOL)horizontal; | |
@end |
This file contains 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
// | |
// KBorderedGradientView.m | |
// | |
// Created by Kalpesh Talkar on 01/02/16. | |
// Copyright © 2016 Kalpesh. All rights reserved. | |
// | |
#import "KBorderedGradientView.h" | |
@interface KBorderedGradientView() | |
@property (nonatomic) IBInspectable CGFloat cornerRadius; | |
@property (nonatomic) IBInspectable CGFloat borderWidth; | |
@property (nonatomic) IBInspectable UIColor *borderColor; | |
@property (nonatomic) IBInspectable UIColor *startColor; | |
@property (nonatomic) IBInspectable UIColor *endColor; | |
@property (nonatomic) IBInspectable BOOL horizontal; | |
@end | |
@implementation KBorderedGradientView | |
#pragma mark - Instance Methods | |
- (instancetype)initWithCoder:(NSCoder *)aDecoder { | |
self = [super initWithCoder:aDecoder]; | |
if (self) { | |
[self renderView]; | |
} | |
return self; | |
} | |
- (instancetype)initWithFrame:(CGRect)frame { | |
self = [super initWithFrame:frame]; | |
if (self) { | |
[self defaults]; | |
[self renderView]; | |
} | |
return self; | |
} | |
- (void)drawRect:(CGRect)rect { | |
[self renderView]; | |
} | |
- (void)setNeedsLayout { | |
[super setNeedsLayout]; | |
[self setNeedsDisplay]; | |
} | |
- (void)prepareForInterfaceBuilder { | |
[self renderView]; | |
} | |
#pragma mark - Defaults | |
- (void)defaults { | |
_cornerRadius = 0.0f; | |
_borderWidth = 0.0f; | |
_borderColor = [UIColor clearColor]; | |
_startColor = [self backgroundColor]; | |
_endColor = [self backgroundColor]; | |
} | |
#pragma mark - Render View | |
- (void)renderView { | |
[self.layer setCornerRadius:_cornerRadius]; | |
[self.layer setBorderWidth:_borderWidth]; | |
[self.layer setBorderColor:[_borderColor CGColor]]; | |
if (_cornerRadius > 0) { | |
[self.layer setMasksToBounds:true]; | |
} | |
[self setGradient]; | |
[self setNeedsDisplay]; | |
} | |
#pragma mark - Set Gradient | |
- (void)setGradient { | |
CAGradientLayer *gradient = [CAGradientLayer layer]; | |
gradient.frame = self.bounds; | |
gradient.colors = [NSArray arrayWithObjects:(id)[_startColor CGColor], (id)[_endColor CGColor], nil]; | |
if (_horizontal) { | |
gradient.startPoint = CGPointMake(0, 0); | |
gradient.endPoint = CGPointMake(1, 0); | |
} else { | |
gradient.startPoint = CGPointMake(0, 0); | |
gradient.endPoint = CGPointMake(0, 1); | |
} | |
[self.layer insertSublayer:gradient atIndex:0]; | |
} | |
#pragma mark - | |
- (void)setCornerRadius:(CGFloat)cornerRadius { | |
_cornerRadius = cornerRadius; | |
[self renderView]; | |
} | |
- (void)setBorderWidth:(CGFloat)borderWidth { | |
_borderWidth = borderWidth; | |
[self renderView]; | |
} | |
- (void)setBorderColor:(UIColor *)borderColor { | |
_borderColor = borderColor; | |
[self renderView]; | |
} | |
- (void)setStartColor:(UIColor *)startColor { | |
_startColor = startColor; | |
[self renderView]; | |
} | |
- (void)setEndColor:(UIColor *)endColor { | |
_endColor = endColor; | |
[self renderView]; | |
} | |
- (void)setGradientColorWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor horizontal:(BOOL)horizontal { | |
_startColor = startColor; | |
_endColor = endColor; | |
_horizontal = horizontal; | |
[self setGradient]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment