I'm trying to use CAGradientLayer to fade foreground controls against the background textured view.
So in a nutshell, I have a view with the bg color as scrollViewTexturedBackgroundColor. I have a view on top of that, whose contents I'd like to fade at the border into the background view's color.
CAGradientLayer* gradient = [CAGradientLayer layer];
gradient.frame = self->scrollableCanvas.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor scrollViewTexturedBackgroundColor] CGColor], (id)[[UIColor scrollViewTexturedBackgroundColor] CGColor], nil];
[gradient setStartPoint:CGPointMake(0.90, 1)];
[gradient setEndPoint:CGPointMake(1, 1)];
[[self->scrollableCanvas layer] addSublayer:gradient];
[[self->scrollableCanvas layer] setMasksToBounds:YES];
Unfortunately, CAGradientLayer doesn't seem to support UIColors as pattern images.
any ideas how I can achieve a simple border fade effect for the subview?
thanks
You can use a little trick:
//create normal UIImageView
UIImageView* iv = [[[UIImageView alloc] initWithImage: [UIImage imageNamed :#"bg_png.png"]] autorelease];
[superview addSubview: iv];
//draw gradient on top
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite: 1.0 alpha: 0.0] CGColor], (id)[[UIColor colorWithWhite: 1.0 alpha: 1.0] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];
[superview addSubview: view];
I made a category on UILabel that adds gradient at the end of the label using Max's answer.
If you need, here it is:
#import "UILabel+GradientEnding.h"
#implementation UILabel (GradientEnding)
- (void)addGradientEnding
{
//draw gradient on top
UIView *gradientAlphaView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width - 80.0, 0, 80.0, self.frame.size.height)];
UIColor *startColor = RGBA(0xf7f7f7, 0);
UIColor *endColor = RGBA(0xf7f7f7, 1);
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = gradientAlphaView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];
gradient.startPoint = CGPointMake(0, 0.5);
gradient.endPoint = CGPointMake(1.0, 0.5);
[gradientAlphaView.layer insertSublayer:gradient atIndex:0];
[self addSubview:gradientAlphaView];
}
#end
But you should use your own colors. And use this define:
#define RGBA(rgbValue, opacity) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:opacity]
Related
I have the following code:
self.navigationBar_.layer.shadowColor = [UIColor blackColor].CGColor;
self.navigationBar_.layer.shadowOpacity = 0.3f;
self.navigationBar_.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
self.navigationBar_.layer.shadowRadius = 3.0f;
self.navigationBar_.layer.masksToBounds = NO;
and I basically only want to add the border to the bottom only and not the entire rectangle. How do I do this? The code above will add a shadow to the left, right, top border also.
Instead of a layer shadow, you could just use a gradient. You could just use a transparent PNG gradient, but here is an example of how to do it programmatically:
UIView *topShadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.navigationBar.bounds.size.width, 10)];
CAGradientLayer *topShadow = [CAGradientLayer layer];
topShadow.frame = CGRectMake(0, 0, self.navigationBar.bounds.size.width, 10);
topShadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.25f] CGColor], (id)[[UIColor clearColor] CGColor], nil];
[topShadowView.layer insertSublayer:topShadow atIndex:0];
[self.view addSubview:topShadowView];
I am adding gradient to navigation Bar' layer and it is working fine.
Issue arrive when I push another view Controller and pop view Controller. Parent view Controller has right Bar Button Item whose color dims when I pop View Controller.
My code is
CGRect navFrame = self.navigationController.navigationBar.frame;
navFrame.origin.y = 0.0f;
[self.navigationController.navigationBar.layer insertSublayer:[AddGradient addGradientToNavigationBar:navFrame] atIndex:0];
code for addGradientToNavigationBar is
+ (CAGradientLayer*)addGradientToNavigationBar:(CGRect)navRect {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = navRect;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0] CGColor],
(id)[[XAppDelegate getColor] CGColor], nil];
return gradient;
}
I have the same problem.
I was tying to add gradient layer to navigation bar with the UIBarStyleBlackTranslucent style for glass effect. After searching the web and a lot of experiments I have not found the right solution for that problem. Finally I've came up with following work around:
CAGradientLayer *yourGradient = ...
// Need two additional layers to mimic UIBarStyleBlackTranslucent style
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, 0, 320, 44);
NSMutableArray *cgColors = [[NSMutableArray alloc] init];
[cgColors addObject:(id)[[UIColor colorWithRed:0.0
green:0.0
blue:0.0
alpha:0.2] CGColor]];
[cgColors addObject:(id)[[UIColor colorWithRed:0.0
green:0.0
blue:0.0
alpha:0.6] CGColor]];
[cgColors addObject:(id)[[UIColor colorWithRed:0.0
green:0.0
blue:0.0
alpha:0.5] CGColor]];
gradient.colors = cgColors;
CALayer *layer = [CALayer layer];
_layer.frame = CGRectMake(0, 22, 320, 22);
_layer.backgroundColor =[[UIColor colorWithRed:0.0
green:0.0
blue:0.0
alpha:0.2] CGColor];
// This is needed for navigation bar buttons
[rootController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
// Adding layers to the first sublayer of the navigation bar layer
[[[rootController.navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:yourGradient atIndex:0];
// After adding the gradient layer, the UIBarStyleBlackTranslucent style effect disappears
// from the bar (but not from the buttons)
// So the following is needed to mimic it
[[[rootController.navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:gradient atIndex:1];
[[[rootController.navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:layer atIndex:2];
you may also want to add some borders to your layer as the borders seems to disappear.
I have a UITableViewCell that I've added a gradient to by using CAGradientLayer. This works fine, but the table cell doesn't turn blue when selected, even after setting it's selectionStyle to UITableViewCellSelectionStyleBlue. If I don't add the gradient layer, it works fine.
Is there a way to make these items work together?
Here's my code inside of cellForRowAtIndexPath:
//cell gradient
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(10, 0, 300, 50);
gradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1] CGColor],
(id)[[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1] CGColor],
(id)[[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1] CGColor],
(id)[[UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1] CGColor],
nil];
gradient.locations = [NSArray arrayWithObjects:
(id)[NSNumber numberWithFloat:0.00],
(id)[NSNumber numberWithFloat:0.50],
(id)[NSNumber numberWithFloat:0.51],
(id)[NSNumber numberWithFloat:1.0],
nil];
[cell.layer insertSublayer:gradient atIndex:0];
//bring back rounded corners by creating a masklayer
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:gradient.bounds byRoundingCorners:UIRectCornerBottomRight|UIRectCornerBottomLeft cornerRadii:CGSizeMake(8, 8)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = gradient.bounds;
maskLayer.path = maskPath.CGPath;
gradient.mask = maskLayer;
//add the cell shadow
cell.layer.shadowColor = [[UIColor blackColor] CGColor];
cell.layer.shadowRadius = 3;
cell.layer.shadowOpacity = 0.5;
cell.layer.shadowOffset = CGSizeMake(0, 0);
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
It looks like you might be putting the gradient over the view that changes when it is selected- the UITableViewCellSelectionStyleBlue view may be appearing, but you just cannot see it (a way to test it out is only have the gradient cover part of your cell- if the other part changes color when it is selected, then this is the issue).
If this is the issue, then you could do your own custom drawing for the cell when it is selected by subclassing UITableViewCell, or you could have the gradient disappear whenever the cell is selected (and reappear when the cell is deselected).
I have a UIView & I want that view's layer to be a continuously looping animation but I'm having some trouble getting started. I have the following in my view subclass:
+ (Class)layerClass {
return [CALayer class];
}
Then in my viewController I have:
CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setColors:[NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor],nil]];
[[[self view] layer] addSublayer:gradient];
This crashes my app. What exactly am I doing wrong? The app crashes w/ EXC_BAD_ACCESS.
Actually the crash you are having isn't related to not setting locations. The issue has to do with the fact that you're passing the wrong types in your setColors array. A CGColorRef is the types that is expected. Your code should look like this:
CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setColors:[NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor],
(id)[[UIColor blueColor] CGColor],nil]];
[[[self view] layer] addSublayer:gradient];
The locations parameter of a CAGradientLayer is optional.
Best regards.
You forget to specify the locations for colors. By default, the location is nil.
CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setColors:[NSArray arrayWithObjects:
[[UIColor redColor] CGColor],
[[UIColor blueColor] CGColor],
nil]];
[gradient setLocations:
[NSNumber numberWithFloat:0.0], // for redColor
[NSNumber numberWithFloat:1.0], // for blueColor
nil]];
[[[self view] layer] addSublayer:gradient];
I have the following code and I can't seem to get it to work.
First I create a view with a layer.
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];
Next I need to replace that layer. I have tried the following and it fails because I don't know how to access that layer 0. At this point I am in a different part of the program so I cannot just call gradient. I need to extricate it from view somehow.
[view.layer replaceSublayer:0 with:newgradient];
Apparently the 0 is supposed to be Old layer but I don't know how to access it.
Thanks.
I think you should be able to use this:
[view.layer replaceSublayer:[[view.layer sublayers] objectAtIndex:0]
with:newGradient];
I'm away from my Xcode development environment and can't test this at the moment though.
Keep a pointer to the gradient layer. If you have alot of them do it with an array.
If you still don't want to do that you can always use this to get an array of all the layers..
[self.layer sublayers];
Then you can cycle through and check some parameter to see if they are the same. Like set the layer.name to something when you create it.
Here is the function :
#define MyGradientLayerName #"MyGradient"
void makeViewGradient(UIView *pView,BOOL bRemoveBackground,CGColorRef clr1,CGColorRef clr2)
{
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.name = MyGradientLayerName;
gradient.frame = pView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)clr1,(id)clr2,nil];
CALayer *pGradientLayer = nil;
NSArray *ar = pView.layer.sublayers;
for (CALayer *pLayer in ar)
{
if ([pLayer.name isEqualToString:MyGradientLayerName])
{
pGradientLayer = pLayer;
break;
}
}
if (!pGradientLayer) [pView.layer insertSublayer:gradient atIndex:0];
else [pView.layer replaceSublayer:pGradientLayer with:gradient];
if (bRemoveBackground) pView.backgroundColor = nil;//free memory !
}