Replace a CALayer with a CAGradientLayer doesn't work - ios

I'm developing an iOS app with latest SDK.
I want to change a background layer dynamically:
#import <QuartzCore/QuartzCore.h>
#interface MyClass : UIView
{
#private
CALayer* _gradientBackground;
}
And some methods:
- (CALayer*)createLayerWithColor:(UIColor*)color
{
CALayer* layer = [CALayer layer];
layer.frame = CGRectMake(NSLayerX, NSLayerY,
NSLayerWidth, NSLayerHeight);
layer.backgroundColor = [color CGColor];
layer.cornerRadius = NSCornerRadius;
return layer;
}
- (CAGradientLayer*)createLayerWithGradient:(UIColor*)startColor
endColor:(UIColor*)endColor
{
CAGradientLayer* gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(NSLayerX, NSLayerY,
NSLayerWidth, NSLayerHeight);
gradientLayer.colors =
[NSArray arrayWithObjects:(id)[startColor CGColor],
(id)[endColor CGColor], nil];
gradientLayer.cornerRadius = NSCornerRadius;
return gradientLayer;
}
- (void)changeBackgroundWithLayer:(CALayer*)newLayer
{
if (_gradientBackground != nil)
[_gradientBackground removeFromSuperlayer];
_gradientBackground = newLayer;
[self.layer insertSublayer:newLayer atIndex:0];
}
And I do this to change background layer:
[self changeBackgroundWithLayer:[self createLayerWithGradient:startColor endColor:endColor]];
And sometimes with this:
[self changeBackgroundWithLayer:[self createLayerWithColor:newColor]];
The way I do is:
First solid layer, next gradient layer and finally solid layer.
I have also tried with this code with no result:
- (void)changeBackgroundWithLayer:(CALayer*)newLayer
{
if (_gradientBackground != nil)
[self.layer replaceSublayer:_gradientBackground with:newLayer];
else
[self.layer insertSublayer:newLayer atIndex:0];
_gradientBackground = newLayer;
}
But it doesn't work.
Any advice?

Try this
- (void)changeBackgroundWithLayer:(CALayer*)newLayer
{
[_gradientBackground removeFromSuperlayer], _gradientBackground = nil;
[self.layer insertSublayer:newLayer atIndex:0];
_gradientBackground = newLayer;
}
This will remove the _gradientBackground and set it to nil in every case, which is perfectly acceptable in Objective-C. The newLayer is added to the layer hierarchy and the gradientBackground layer is updated.

Related

CAGradientLayer with shouldRasterize causes unwanted line

I use CAGradientLayer in header view of TableView. To make scroll smooth I've turned shouldRasterize property to YES and it's caused the problem.
Gradient layer is drawn like it has a border. All frames I set in layoutSubviews and all its fields are integers.
Here are screenshots:
How it appears:
How it have to look like:
Here is a code related to this gradient layer
- (void)createShadow {
CAGradientLayer *shadowLayer= [CAGradientLayer layer];
UIColor *startColor = [UIColor colorWithWholeRed:236 green:235 blue:236];
UIColor *destinationColor = [UIColor colorWithWholeRed:248 green:248 blue:250];
shadowLayer.borderColor = [UIColor clearColor].CGColor;
shadowLayer.borderWidth = 0;
shadowLayer.colors = #[(id)startColor.CGColor,(id)destinationColor.CGColor];
shadowLayer.shouldRasterize = YES;
[self.layer addSublayer:shadowLayer];
self.shadowLayer = shadowLayer;
}
- (void)updateShadow {
CGRect newRect = CGRectMake(0, 0,
self.frame.size.width, ceil(self.frame.size.height * 0.3));
[self.shadowLayer setFrame:newRect];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self updateShadow];
}
Tell me please if you know how to solve this problem)

How to avoid infinite layoutSubviews

I have a UITableViewCell subclass which overrides layoutSubviews to apply a fade effect to a webview. For the most part it works fine, but sometimes it ends up in a state where layoutSubvies is called constantly with alternating values for the content height for the webview.
Is there some other place I could apply this gradient to avoid this issue?
CAGradientLayer* gradientLayer = nil;
int webViewHeight = 0;
- (void)layoutSubviews
{
NSLog(#"layoutSubviews %f", self.webView.scrollView.contentSize.height);
[super layoutSubviews];
if (self.webView.scrollView.contentSize.height == webViewHeight) {
// nothing has changed
return;
}
if (gradientLayer != nil) {
[gradientLayer removeFromSuperlayer];
gradientLayer = nil;
}
// if cell height is less than web view height we must fade
if (self.frame.size.height < self.webView.scrollView.contentSize.height) {
webViewHeight = self.webView.scrollView.contentSize.height;
gradientLayer = [CAGradientLayer layer];
NSObject* fadeColor = (NSObject*)[UIColor colorWithWhite:1.0 alpha:.9].CGColor;
NSObject* transparentColor = (NSObject*)[UIColor colorWithWhite:1.0 alpha:0.0].CGColor;
gradientLayer.colors = [NSArray arrayWithObjects:transparentColor, fadeColor, nil];
gradientLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.5],
[NSNumber numberWithFloat:1.0], nil];
gradientLayer.bounds = self.bounds;
gradientLayer.anchorPoint = CGPointZero;
[self.layer addSublayer:gradientLayer];
}
}
my TWO cents for similar issue. Hope can help others.
After debugging a little, I noted ALSO adding CALayer will re-invoke layoutSubviews
So if You call:
self.layer.addSublayer(anotherLayer)
you wil be re-called in layoutSubviews.

Rotation background gradient xcode

I have a problem with my background gradient on my app. I set in viewDidLoad in MainViewController.
CAGradientLayer *bgLayer = [BackgroundGradient blackGradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
and every UIViewController extends from MainViewController. It works fine but after rotate device it show me only half background. Other is white. Can you help? Where can be the problem and show me it only half after rotate?
In BackgroundGradient.m
#implementation BackgroundGradient
+ (CAGradientLayer*) blackGradient {
UIColor *colorOne = [UIColor colorWithRed:0.12 green:0.13 blue:0.13 alpha:1.0];
UIColor *colorTwo = [UIColor colorWithRed:0.11 green:0.12 blue:0.13 alpha:1.0];
NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
NSNumber *stopTwo = [NSNumber numberWithFloat:1.0];
NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil];
CAGradientLayer *headerLayer = [CAGradientLayer layer];
headerLayer.colors = colors;
headerLayer.locations = locations;
return headerLayer;
}
Save your layer into controller property, than override layoutSubviews method and update layer frame.
- (void)layoutSubviews {
[super layoutSubviews];
self.backgroundLayer.frame = self.view.bounds;
}

Change UIButton gradient color

I use the following method to add some gradient effect to my IOS UIButton. I call it in viewController's viewDidLoad, works great, so far so good.
Now, I'd like to change the color of the button according to some user interaction. What I do is simply calling this method by different colors (fromColor, toColor) as input. The problem, the look of my button does not change. I tried to call setNeedsDisplay, but did not help.
Would you be so kind to help me? What I miss? There is should be some problem with layers, should be rested or something like that, but I could not find its proper way.
Update: By using setSubLayers instead of insertSubLayer color changes, but button title disappears.
+(void)setCustomButtonStyle:(UIButton *)button cornerRadius:(float) cornerRadius fromColor:(UIColor *)fromColor toColor:(UIColor *)toColor normalTitleColor:(UIColor *)normalTitleColor highlightedTitleColor:(UIColor *)highlightedTitleColor borderWidth:(float) borderWidth borderColor:(UIColor*)borderColor
{
[button setTitleColor:normalTitleColor forState:UIControlStateNormal];
[button setTitleColor:highlightedTitleColor forState:UIControlStateHighlighted];
CAGradientLayer *btnGradient = [CAGradientLayer layer];
btnGradient.frame = button.bounds;
btnGradient.colors = [NSArray arrayWithObjects:
(id)[fromColor CGColor],
(id)[toColor CGColor],
nil];
btnGradient.borderWidth = borderWidth;
btnGradient.borderColor = [borderColor CGColor];
[button.layer insertSublayer:btnGradient atIndex:0];
CALayer *btnLayer = [button layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:cornerRadius];
}
The problem you are having here is that 1. you are creating a new layer each time you are changing the colour and 2. you are then placing that new layer underneath everything else so you can't see it anyway. (Think of putting a playing card at the bottom of the deck).
What you need to do is keep hold of the gradient layer and then update then instead of creating a new one.
You are probably best doing this in a category on UIButton.
Something like... UIButton+GradientBackground or something.
In the .h file give it a single function...
// Use CGFloat not float. Also user NSInteger not int.
// Always.
// Also, I have used modern Obj-C syntax. Use this too.
- (void)setCornerRadius:(CGFloat)cornerRadius
fromColor:(UIColor *)fromColor
toColor:(UIColor *)toColor
normalTitleColor:(UIColor *)normalTitleColor
highlightedTitleColor:(UIColor *)highlightedTitleColor
borderWidth:(CGFloat)borderWidth
borderColor:(UIColor *)borderColor;
Then in the .m file you need to also give it a property...
#property (nonatomic, strong) CAGradientLayer *gradientLayer;
Now in the function...
- (void)setCornerRadius:(CGFloat)cornerRadius
fromColor:(UIColor *)fromColor
toColor:(UIColor *)toColor
normalTitleColor:(UIColor *)normalTitleColor
highlightedTitleColor:(UIColor *)highlightedTitleColor
borderWidth:(CGFloat)borderWidth
borderColor:(UIColor *)borderColor
{
[self setTitleColor:normalTitleColor forState:UIControlStateNormal];
[self setTitleColor:highlightedTitleColor forState:UIControlStateHighlighted];
if (!self.grandientLayer) {
// if it doesn't exist then create it and add it (only once)
self.gradientLayer = [CAGradientLayer layer];
[self.layer insertSublayer:self.gradientLayer atIndex:0];
}
self.gradientLayer.frame = self.bounds;
self.gradientLayer.colors = #[(id)fromColor.CGColor, (id)toColor.CGColor]; // use modern syntax
self.gradientLayer.borderWidth = borderWidth;
self.gradientLayer.borderColor = borderColor.CGColor;
[self.layer setMasksToBounds:YES];
[self.layer setCornerRadius:cornerRadius];
}
Notice that I have removed the button from the function name as this function will essentially be part of that UIButton's functions so self is the button you are changing.
You would call it like this...
[someButton setCornerRadius:5 fromColor:[UIColor redColor] toColor... and so on];
By doing this you only create that gradient layer once and you keep hold of it so that you can update it the next time round.
For first time add gradient layer, next time replace added gradient layer
Added Category as #Fogmeister thinks this can be used with category only
#interface UIButton (gradient)
+(void)setCustomButtonStyle:(UIButton *)button cornerRadius:(float) cornerRadius fromColor:(UIColor *)fromColor toColor:(UIColor *)toColor normalTitleColor:(UIColor *)normalTitleColor highlightedTitleColor:(UIColor *)highlightedTitleColor borderWidth:(float) borderWidth borderColor:(UIColor*)borderColor;
#end
#implementation UIButton (gradient)
+(void)setCustomButtonStyle:(UIButton *)button cornerRadius:(float) cornerRadius fromColor:(UIColor *)fromColor toColor:(UIColor *)toColor normalTitleColor:(UIColor *)normalTitleColor highlightedTitleColor:(UIColor *)highlightedTitleColor borderWidth:(float) borderWidth borderColor:(UIColor*)borderColor
{
//Set Title color as you want
[button setTitleColor:normalTitleColor forState:UIControlStateNormal];
[button setTitleColor:highlightedTitleColor forState:UIControlStateHighlighted];
//check gradientlayer exist
id layer = nil;
if (button.layer.sublayers.count > 0) {
layer = [button.layer.sublayers objectAtIndex:0];
}
//Find added gradient layer
CAGradientLayer *addedGradLayer = nil;
if (layer) {
if ([layer isKindOfClass:[CAGradientLayer class]]) {
addedGradLayer = (CAGradientLayer *)[button.layer.sublayers objectAtIndex:0];
}
}
//check gradient layer exists
if (!addedGradLayer) //first time
{
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = button.layer.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:
(id)fromColor.CGColor,
(id)toColor.CGColor,
nil];
gradientLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:1.0f],
nil];
gradientLayer.cornerRadius = button.layer.cornerRadius;
[button.layer insertSublayer:gradientLayer atIndex:0];
button.clipsToBounds = YES;
}
else //next time
{
CAGradientLayer *newGradientLayer = [CAGradientLayer layer];
newGradientLayer.frame = button.layer.bounds;
newGradientLayer.colors = [NSArray arrayWithObjects:
(id)fromColor.CGColor,
(id)toColor.CGColor,
nil];
newGradientLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:1.0f],
nil];
newGradientLayer.cornerRadius = button.layer.cornerRadius;
[button.layer replaceSublayer:addedGradLayer with:newGradientLayer];
}
}
#end

How to disable Customized UIButton

I subclassed UIButton and did some customized drawing in drawRect method such as drawing NSAttributedString and UIImage.
However, after I did this, the customized UIButton doesn't gray out when enabled is set to NO. I think my customized drawing happens on top of its state. How do I deal with this?
Sharing my drawing code here:
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (self.faceUp) {
[self drawCardText:self.card.contents inRect:self.bounds];
} else {
[self drawCardImage:[UIImage imageNamed:CardBackImageName] inRect:self.bounds];
}
}
- (void)drawCardText:(NSString *)text inRect:(CGRect)rect
{
// set background color to white so text can be shown
[[UIColor whiteColor] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
UIFont *preferedFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
UIFont *actualFont = [UIFont fontWithName:preferedFont.fontName
size:hypotf(rect.size.width, rect.size.height) / 6.0];
NSDictionary *attributes = #{NSFontAttributeName: actualFont};
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
[self setAttributedTitle:attributedText forState:UIControlStateNormal];
}
- (void)drawCardImage:(UIImage *)image inRect:(CGRect)rect
{
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0f);
[image drawInRect:rect];
UIImage *actualImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UIColor colorWithPatternImage:actualImage] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
}
You might have to add an overlay view with grey color and some alpha yourself. Remove the overlay when button is enabled again.
You can add this method to CustomButton class.
-(void)setEnabled:(BOOL)enabled{
//disableLayer.hidden = !enabled;
if (enabled) {
//self.enabled = YES;
self.alpha = 1.0;
}else{
//self.enabled = NO;
self.alpha = 0.7;
}
[super setEnabled:enabled];
}
For enable or disable call-
[customButtob setEnabled:buttonStatus];
If you want to change color, add a background layer, and toggle its hidden property in setEnabled method.
disableLayer = [CALayer layer];
disableLayer.backgroundColor = [UIColor colorWithRed:20/255.0f green:20/255.0f blue:20/255.0f alpha:1.0f].CGColor;
disableLayer.frame = self.layer.bounds;
disableLayer.hidden = YES;
[self.layer insertSublayer:disableLayer below:otherLayer];
I think you share some more details of your implementation. Moreover I am sure you have called
Super methods in the customized methods at first call.
You have to override the setEnabled: method of your CustomButton
-(void)setEnabled:(BOOL)enabled
{
if (!enabled) {
[self setAlpha:0.2f];
}
}
OR
If you have to change the color of UIButton in its disabled state. Yo can change drawRect: method as
Make a assign property of BOOL type variable (say isEnabled)
-(void)drawRect:(CGRect)rect
{
[[UIColor redColor] setFill];
UIRectFill(rect);
if (!self.isEnabled) {
[[UIColor blackColor] setFill];
UIRectFill(rect);
}
}
-(void)setEnabled:(BOOL)enabled
{
self.isEnabled = enabled;
[self setNeedsDisplay];
}
You can use
btn.userInteractionEnabled = NO;
Or you can use
btn.enabled = NO;
Hope this helps... :)
Edit:
I think you can use this
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
Add this:
-(void)setEnabled:(BOOL)enabled{
if (!enabled) {
self.alpha = 0.3;
}else{
self.alpha = 1.0;
}
[super setEnabled:enabled];
}
add a method which can help u for enable and disable of your button just add this method in custom button class
//in customButton.h file
#import <UIKit/UIKit.h>
#interface CustomButton : UIButton
#property (nonatomic,assign) BOOL faceUp;//your property
- (void)makeButtonDisable:(BOOL)disable;//method to handle disable and enable
#end
//in customButton.m file
#import "CustomButton.h"
#implementation CustomButton
{
CALayer *grayLayer;//layer to handle disable and enable
}
#synthesize faceUp;
// in initilization method
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
grayLayer = [CALayer layer];
grayLayer.frame = frame;
[self.layer addSublayer:grayLayer]; //add the grayLayer during initialisation
}
return self;
}
//your code that put above
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (self.faceUp) {
[self drawCardText:self.card.contents inRect:self.bounds];
} else {
[self drawCardImage:[UIImage imageNamed:CardBackImageName] inRect:self.bounds];
}
}
- (void)drawCardText:(NSString *)text inRect:(CGRect)rect
{
// set background color to white so text can be shown
[[UIColor whiteColor] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
UIFont *preferedFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
UIFont *actualFont = [UIFont fontWithName:preferedFont.fontName
size:hypotf(rect.size.width, rect.size.height) / 6.0];
NSDictionary *attributes = #{NSFontAttributeName: actualFont};
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
[self setAttributedTitle:attributedText forState:UIControlStateNormal];
}
- (void)drawCardImage:(UIImage *)image inRect:(CGRect)rect
{
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0f);
[image drawInRect:rect];
UIImage *actualImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UIColor colorWithPatternImage:actualImage] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
}
//add this method
- (void)makeButtonDisable:(BOOL)disable
{
if(disable)
{
grayLayer.backgroundColor = [UIColor colorWithRed:201.0f/255.0f green:201.0f/255.0f blue:201.0f/255.0f alpha:5.0f].CGColor;
self.userInteractionEnabled = NO;
grayLayer.opacity = 0.5f;
self.alpha = 0.5f;
}
else
{
grayLayer.backgroundColor = [UIColor clearColor].CGColor;
self.userInteractionEnabled = YES;
grayLayer.opacity = 0.0f;
self.alpha = 1.0f;
}
}
in the controller where u are using this button
//some where in the controller u want to disable the button just do like this
//to disable
[button makeButtonDisable:YES]; //button is an instance of customCell
//to enable
[button makeButtonDisable:NO]; //button is an instance of customCell

Resources