Skip to content

Instantly share code, notes, and snippets.

@mathiarasan24
Last active August 10, 2017 10:40
Show Gist options
  • Save mathiarasan24/e8ae6b498eefdae97a7afbcea9f07f59 to your computer and use it in GitHub Desktop.
Save mathiarasan24/e8ae6b498eefdae97a7afbcea9f07f59 to your computer and use it in GitHub Desktop.
MAi Objective-C CALayer category file
//
// CALayer+MAi.h
//
// Created by User on 31/07/17.
// Copyright © 2017 Mathi Arasan. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
/*!
* @abstract Categories class for CALayer
* @discussion Draw border layer with default or custom frame and color
*/
@interface CALayer (MAi)
/*!
* A really simple way to draw layer with default color and frame.
* @abstract Instance class fucntion for CALayer
* @brief By default (Like in UITableViewSeparator) layer color with top possition
* @discussion Color: lightGrayColor
* @discussion Frame: X = 0, Y = 0, Width = Screen width, Height = 1
* @return _Nonnull CALayer
* @code [view.layer addSublayer:[CALayer drawTopLayerWithDefaultColorAndFullWidth]];
*/
+(CALayer *_Nonnull)drawTopLayerWithDefaultColorAndFullWidth;
/*!
* A really simple way to draw layer with default color and custom frame.
* @abstract Instance class fucntion for CALayer
* @brief By default (Like in UITableViewSeparator) layer color with custom frame
* @discussion Color: lightGrayColor
* @param frame: CGRect frame (non CGRectZero)
* @return _Nonnull CALayer
* @code [view.layer addSublayer:[CALayer drawLayerWithDefaultColorAndCustomFrame:CGRectMake(x, y, width, height)]];
*/
+(CALayer *_Nonnull)drawLayerWithDefaultColorAndCustomFrame:(CGRect)frame;
/*!
* A really simple way to draw layer with custom color and default frame.
* @abstract Instance class fucntion for CALayer
* @brief By default (Like in UITableViewSeparator) layer custom color with default frame
* @discussion Frame: X = 0, Y = 0, Width = Screen width, Height = 1
* @param color: _Nonnull UIColor
* @return _Nonnull CALayer
* @code [view.layer addSublayer:[CALayer drawTopLayerWithFullWidthAndCustomColor:[UIColor greenColor]];
*/
+(CALayer *_Nonnull)drawTopLayerWithFullWidthAndCustomColor:(UIColor *_Nonnull)color;
/*!
* A really simple way to draw layer with custom color and custom frame.
* @abstract Instance class fucntion for CALayer
* @brief By default (Like in UITableViewSeparator) layer custom color with custom frame
* @param color: _Nonnull UIColor
* @param frame: CGRect frame (non CGRectZero)
* @return _Nonnull CALayer
* @code [view.layer addSublayer:[CALayer drawTopLayerWithCustomColor:[UIColor greenColor] customFrame:CGRectMake(x, y, width, height)]];
*/
+(CALayer *_Nonnull)drawTopLayerWithCustomColor:(UIColor *_Nonnull)color customFrame:(CGRect)frame;
/*!
* A really simple way to draw back button image '<'
* @abstract Instance class fucntion for CALayer
* @brief By default (Like in apple back button image) '<' with custom color
* @warinig Layer super view should be in width and height = 44.0
* @return _Nonnull CALayer
* @code [button addSublayer:[CALayer drawBackButtonLayerWithColor:[UIColor whiteColor]]];
*/
+(CALayer *_Nonnull)drawBackButtonLayerWithColor:(UIColor *_Nonnull)color;
/*!
* A really simple way to retrive layer based on name search
* @abstract Instance class fucntion for CALayer
* @discussion Search in the view and retrive layer equivalent to given name
* @warinig Layer super view should be in width and height = 44.0
* @param layerName: _Nonnull layer name
* @param aView: _Nonnull layer superview
* @return _Nullable CALayer
* @code CALayer *aLayer = [CALayer retriveLayerFromName:@"layerName" forView:layerSuperView];
*/
+(CALayer *_Nullable)retriveLayerFromName:(NSString *_Nonnull)layerName forView:(UIView *_Nonnull)aView;
/*!
* @brief Make layer with corner radius with custom value
* @param value: Layer radius value
* @return _Nullable CALayer
* @code [view.layer makeCornerRadiusValue:4.];
*/
-(CALayer *_Nonnull)makeCornerRadiusValue:(CGFloat)value;
@end
//
// CALayer+MAi.m
//
// Created by User on 31/07/17.
// Copyright © 2017 Mathi Arasan. All rights reserved.
//
#import "CALayer+MAi.h"
@implementation CALayer (MAi)
#pragma mark - CALayer
+(CALayer *_Nonnull)drawTopLayerWithDefaultColorAndFullWidth
{
return [self drawLayerWithColor:[UIColor lightGrayColor] frame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1)];
}
+(CALayer *_Nonnull)drawLayerWithDefaultColorAndCustomFrame:(CGRect)frame
{
return [self drawLayerWithColor:[UIColor lightGrayColor] frame:frame];
}
+(CALayer *_Nonnull)drawTopLayerWithFullWidthAndCustomColor:(UIColor *_Nonnull)color
{
return [self drawLayerWithColor:color frame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1)];
}
+(CALayer *_Nonnull)drawTopLayerWithCustomColor:(UIColor *_Nonnull)color customFrame:(CGRect)frame
{
return [self drawLayerWithColor:color frame:frame];
}
+(CALayer *_Nonnull)drawLayerWithColor:(UIColor *_Nonnull)color frame:(CGRect)frame
{
CALayer *customLayer = [CALayer layer];
customLayer.backgroundColor = color.CGColor;
customLayer.frame = frame;
return customLayer;
}
+(CALayer *_Nonnull)drawBackButtonLayerWithColor:(UIColor *_Nonnull)color
{
CGFloat backButtonHeightAndWidth = 44.;
CGFloat midValue = (backButtonHeightAndWidth)/2.;
CGFloat fourOfValue = (backButtonHeightAndWidth)/4;
UIColor *shapeColor = [UIColor whiteColor];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(midValue, fourOfValue)];
[bezierPath addLineToPoint:CGPointMake(fourOfValue, midValue)];
[bezierPath addLineToPoint:CGPointMake(midValue, midValue + fourOfValue)];
[shapeColor setStroke];
[bezierPath stroke];
shapeLayer.lineWidth = 3.;
shapeLayer.lineJoin = kCALineJoinRound;
shapeLayer.path = bezierPath.CGPath;
shapeLayer.backgroundColor = [UIColor clearColor].CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.strokeColor = color.CGColor;
return shapeLayer;
}
+(CALayer *_Nullable)retriveLayerFromName:(NSString *_Nonnull)layerName forView:(UIView *_Nonnull)aView
{
for (CALayer *layer in aView.layer.sublayers)
{
if ([layer.name isEqualToString:layerName]) {
return layer;
}
}
return nil;
}
-(CALayer *_Nonnull)makeCornerRadiusValue:(CGFloat)value
{
self.masksToBounds = YES;
self.cornerRadius = 4.;
self.contentsScale = [[UIScreen mainScreen] scale];
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment