How to remove CAGradientLayer from UIButton when it's highlighted - ios

I've added gradient effect for my Button and added it as a sub layer to my Button.
Code is :
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.myButton.layer.bounds;
gradientLayer.colors = [NSArray arrayWithObjects: (id)[[UIColor whiteColor] colorWithAlphaComponent:0.5].CGColor,(id) colorTwo.CGColor,(id) colorThree.CGColor,(id) colorFour.CGColor, nil];
gradientLayer.locations = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:1.0f], nil];
self.myButton.layer.cornerRadius = 5;
gradientLayer.cornerRadius = self.myButton.layer.cornerRadius;
[self.myButton.layer addSublayer:gradientLayer];
Now when my button is highlighted (touchedDown method called), I want to set new gradient to myButton. Hence I need to remove the old gradient effect when myButton is clicked and add new gradient.
How to remove the old gradient effect ?
I tried
for (CALayer *layer in [self.loginScreenView.btnPowerBroking.layer.sublayers copy]){
if ([[layer name] isEqualToString:#"gradientLayer"]) {
NSLog(#"Gradient layer found.");
}
}
But I can't get it's gradient layer.
Thanks for your time. (:

You haven't set the name property to your gradientLayer object just set it and it will work.
gradientLayer.name = #"gradientLayer";
[self.myButton.layer addSublayer:gradientLayer];
In for loop's if statement remove the layer using this [layer removeFromSuperlayer];
Note : You are adding layer in myButton object in searching inside the btnPowerBroking check properly for that also, you need to search in button where you have added Layer.

Try this method...
[self.myButton.layer replaceSublayer:oldgradientLayer with:newgradientLayer];

Related

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.

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

I couldn't draw fully gradient background on iPad

I drew gradient background layer on UICollectionView with following code.
CAGradientLayer* collectionRadient = [CAGradientLayer layer];
collectionRadient.bounds = self.collectionView.bounds;
collectionRadient.anchorPoint = CGPointZero;
collectionRadient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor],(id)[endColor CGColor], nil];
UIView* vv = [[UIView alloc] init];
self.collectionView.backgroundView = vv;
[self.collectionView.backgroundView.layer insertSublayer:collectionRadient atIndex:0];
It works on iPhone simulator. but not iPad.
Below Image is that. the background layer is taking up only a part of UICollectionView.
I would suggest creating a subclass of UIView named GradientView or something similar which you would use as your background view.
In your implementation of GradientView you just need to overload the following class method:
+ (Class)layerClass
{
return [CAGradientLayer class];
}
Then, change your code above to the following:
UIView* backgroundView = [[GradientView alloc] initWithFrame:self.collectionView.bounds];
backgroundView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.collectionView.backgroundView = backgroundView;
CAGradientLayer* collectionGradient = (CAGradientLayer *)backgroundView.layer;
collectionGradient.anchorPoint = CGPointZero;
collectionGradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor],(id) [endColor CGColor], nil];
I'm hoping that your custom GradientView's layer will automatically resize. If not, you may need to implement the layoutSubviews method in your custom GradientView to resize your gradient layer.

UIButton with a gradient layer slow

I am customizing the UIButton with GradientLayer. I see a performance issue as view loading slowly and looks like a Jerk effect. Also when changing the orientation its same. I am using this code for an iPad application. With normal UIButton its smooth loading / rotating.
I am using the sample code found in google (GradientButton class) and is working fine. The container UIViewController which is pushed onto navigationcontroller stack to display the screen.
Here is the code:
- (void)awakeFromNib {
[self initLayers];
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initLayers];
}
return self;
}
- (void)initLayers {
[self initBorder];
[self addShineLayer];
[self addHighlightLayer];
self.clipsToBounds = YES;
}
- (void)initBorder {
CALayer *layer = self.layer;
layer.cornerRadius = 8.0f;
layer.masksToBounds = YES;
layer.borderWidth = 1.0f;
layer.borderColor = [UIColor colorWithWhite:0.5f alpha:0.2f].CGColor;
}
- (void)addShineLayer {
shineLayer = [CAGradientLayer layer];
shineLayer.frame = self.layer.bounds;
shineLayer.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
(id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
(id)[UIColor colorWithWhite:0.75f alpha:0.2f].CGColor,
(id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor,
(id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
nil];
shineLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.5f],
[NSNumber numberWithFloat:0.5f],
[NSNumber numberWithFloat:0.8f],
[NSNumber numberWithFloat:1.0f],
nil];
// shineLayer.shouldRasterize = YES;
// shineLayer.rasterizationScale = [UIScreen mainScreen].scale;
[self.layer addSublayer:shineLayer];
}
#pragma mark -
#pragma mark Highlight button while touched
- (void)addHighlightLayer {
highlightLayer = [CALayer layer];
highlightLayer.backgroundColor = [UIColor colorWithRed:0.25f green:0.25f blue:0.25f alpha:0.75].CGColor;
highlightLayer.frame = self.layer.bounds;
highlightLayer.hidden = YES;
[self.layer insertSublayer:highlightLayer below:shineLayer];
}
- (void)setHighlighted:(BOOL)highlight {
highlightLayer.hidden = !highlight;
[super setHighlighted:highlight];
}
Is the performance issue due to Gradient Layer ?? Please tell me how to rectify this issue.
Try commenting out the different property assignments in your code one at a time and see if you get any performance benefit. I worked on an app once that was getting about 12FPS in core animation when I used setCornerRadius on the layer compared to nearly full frame rate when I turned that off. The issue may be your gradient, however, you won't know for sure unless you just do some a/b comparisons turning off/on the various properties.
I'll tell you right now, though, that when I've used gradient layers on buttons in table view cells, I was able to speed up the table scrolling dramatically when I just used a background image for the button instead of a gradient layer. You can try to turn on shouldRasterize, however, make sure you do that at the top level of your layer tree and not on any sublayers (although the issues that I saw with setting that parameter on all layers may have been fixed in iOS6. I just haven't tested it).

Grandient background for a view on iOS

I'm pretty new to iOS developpement, and I'm trying to set my view background to a gradient.
I created a CAGradientLayer and try to set it. It goes throught building, but my app crashes as soon as it open, throwing a "EXC_BAD_ACCESS".
Here is my code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CAGradientLayer *bgLayer = [CAGradientLayer layer];
UIColor *grey = [UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1.0];
UIColor *black = [UIColor colorWithRed:(180/255.0) green:(180/255.0) blue:(180/255.0) alpha:1.0];
NSNumber *top = [NSNumber numberWithFloat:0.0];
NSNumber *bot = [NSNumber numberWithFloat:1.0];
bgLayer.colors = [NSArray arrayWithObjects:grey, black, nil];
bgLayer.locations = [NSArray arrayWithObjects:top, bot, nil];
bgLayer.frame = self.view.bounds;
[self.view.layer addSublayer:bgLayer];
}
I think I understood that it comes from a bad memory management, but don't what I did wrong. Thanks for your answers in advance.
Note what the docs say about the colors:
An array of CGColorRef objects defining the color of each gradient stop.

Resources