#define kValidDirections [NSArray arrayWithObjects: @"top", @"bottom", @"left", @"right",nil]
- (void) insertLayertoView:(UIView*)sourceView WithRadius:(float)radius Color:(UIColor *)color Directions:(NSArray *)directions
{// Ignore duplicate direction
NSMutableDictionary *directionDict = [[NSMutableDictionary alloc] init];
for (NSString *direction in directions) [directionDict setObject:@"1" forKey:direction];
for (NSString *direction in directionDict) {
// Ignore invalid direction
if ([kValidDirections containsObject:direction])
{
CAGradientLayer *shadow = [CAGradientLayer layer];
if ([direction isEqualToString:@"top"]) {
[shadow setStartPoint:CGPointMake(0.5, 0.0)];
[shadow setEndPoint:CGPointMake(0.5, 1.0)];
shadow.frame = CGRectMake(0, 0, sourceView.bounds.size.width, radius);
}
else if ([direction isEqualToString:@"bottom"])
{
[shadow setStartPoint:CGPointMake(0.5, 1.0)];
[shadow setEndPoint:CGPointMake(0.5, 0.0)];
shadow.frame = CGRectMake(0, sourceView.bounds.size.height - radius, sourceView.bounds.size.width, radius);
} else if ([direction isEqualToString:@"left"])
{
shadow.frame = CGRectMake(0, 0, radius, sourceView.bounds.size.height);
[shadow setStartPoint:CGPointMake(0.0, 0.5)];
[shadow setEndPoint:CGPointMake(1.0, 0.5)];
} else if ([direction isEqualToString:@"right"])
{
shadow.frame = CGRectMake(sourceView.bounds.size.width - radius, 0, radius, sourceView.bounds.size.height);
[shadow setStartPoint:CGPointMake(1.0, 0.5)];
[shadow setEndPoint:CGPointMake(0.0, 0.5)];
}
shadow.colors = [NSArray arrayWithObjects:(id)[color CGColor], (id)[[UIColor clearColor] CGColor], nil];
[sourceView.layer insertSublayer:shadow atIndex:0];
}
}
}