Glow effect to custom UIButton like info UIButton - iphone

As I am new in iphone development. I want to give glow effect like "info UIButton" to "custom UIButton".
-->
Please help me in this case, to give the above effect on button tap.

I found an easy way to do as I want.
It's by enabling check mark of "Shows Touch On Highlight" property.
See the screenshot..
It's working fine for my custom buttons.

if you do not want to use image then use below code
import 'CoreGraphics framework' to use below code
-(IBAction)myButtonTapped:(id)sender
{
UIButton *tmpbtn=(UIButton *)sender;
tmpbtn.layer.shadowColor = [UIColor cyanColor].CGColor;
tmpbtn.layer.shadowRadius = 10.0f;
tmpbtn.layer.shadowOpacity = 1.0f;
tmpbtn.layer.shadowOffset = CGSizeZero;
}
To remove glow effect just add
-(void)removeBtnGlow
{
myBtn.layer.shadowColor = [UIColor clearColor].CGColor;
myBtn.layer.shadowRadius = 10.0f;
myBtn.layer.shadowOpacity = 1.0f;
myBtn.layer.shadowOffset = CGSizeZero;
}

you cut the image of glow effect like and use this code
[yourBtn setBackgroundImage:[UIImage imageNamed:#"yourimagename"] forState:UIControlStateHighlighted];

Have you tried to create an image of a glowing version of your button icon and set it in code like this:
[yourButton setImage:glowImage forState:UIControlStateHighlighted];
or in the interface builder:

As a UIBUtton category and by using glowWithProgress: you can animate glow change
#implementation UIButton (Extra)
-(void)addGlow{
[self glowWithProgress:1.0];
}
-(void)removeGlow{
[self glowWithProgress:0.0];
}
-(void)glowWithProgress:(CGFloat)progress{
CGFloat realProgress = MAX(.0f, MIN(1.0f, progress));
self.layer.shadowColor = [UIColor whiteColor].CGColor;
self.layer.shadowRadius = 10.0f;
self.layer.shadowOpacity = 1.0f * realProgress;
self.layer.shadowOffset = CGSizeZero;
}
#end

Related

Hide programmatically created UISlider

I've programmatically created a UISlider in viewDidLoad using the following code and when a button is pressed I want to hide the object and maybe use it again. I can't seem to get it to work. I've tried a number of approaches which all build correctly but none of which have the desired effect.
CGRect frame1 = CGRectMake(-5.0, 290.0, 100.0, 10.0);
UISlider *sliderSaveurFloral = [[UISlider alloc] initWithFrame:frame1];
[sliderSaveurFloral addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[sliderSaveurFloral setBackgroundColor:[UIColor clearColor]];
sliderSaveurFloral.minimumValue = 0.0;
sliderSaveurFloral.maximumValue = 50.0;
sliderSaveurFloral.continuous = YES;
sliderSaveurFloral.value = 0.0;
[self.view addSubview:sliderSaveurFloral];
CGAffineTransform trans2 = CGAffineTransformMakeRotation(M_PI * -0.5);
sliderSaveurFloral.transform = trans2;
[sliderSaveurFloral setValue:0];
UISlider inherits from UIView. I believe you can set the hidden property to make it invisible.
sliderSaveurFloral.hidden = YES; //Set it back to NO when you want it appear again
You may also have to make it not interactive when hidden,
sliderSaveurFloral.userInteractionEnabled = NO; //Set it back to YES later when you need

UIButton - changing frame alters response to touches

I have a UIButton. I found that when I moved it, it no longer responded to touches. Let me explain in detail how I determined this: when I shift a 100pixel width button by 50 pixels to the right, only the left half responded to my finger tapping it.
How do I tell the button "update your touches checker" when I dynamically change the frame of it? I am changing the frame in code, NOT in xib.
Initialization:
MATCGlossyButton *repeat = [[MATCGlossyButton alloc] init];
[repeat addTarget:self action:#selector(repeatPressed:) forControlEvents:UIControlEventTouchUpInside];
[repeat setTitle:#"Repeat" forState:UIControlStateNormal];
repeat.buttonColor = [UIColor brownColor];
repeat.backgroundColor = [UIColor clearColor];
repeat.cornerRadius = 20;
repeat.frame = CGRectMake(200,208,105,50);
And then later on:
repeat.frame = CGRectMake(265, 208, 105, 50);
Is it possible you are moving the button beyond the bounds of its parent view? Even if clipping is off, touches aren't propagated outside of a view.
An easy way to visualise the borders of your views is to do the folowing:
#import <QuartzCore/QuartzCore.h>
[...]
view.layer.borderWidth = 1;
view.layer.borderColor = [UIColor redColor].CGColor;

Rounding color around borders of a button

I've got a little problem as seen below:
The cell has a background color, and the button doesn't. Still, it doesn't give me rounded edges, but corners. How can I fix that?
UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[[meerKnop layer] setCornerRadius:8.0f];
meerKnop.backgroundColor = [UIColor whiteColor];
meerKnop.frame = CGRectMake(11.0, (60.0 + (teller * 52.5)), 299.0, 50.0);
...
[meerKnop addSubview:locationLabel];
...
[meerKnop addSubview:categoryLabel];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[meerKnop addGestureRecognizer:swipe];
[swipe release];
[meerKnop addTarget:self action:#selector(alertPressed:) forControlEvents:UIControlEventTouchUpInside];
meerKnop.tag = incId;
[cell addSubview:meerKnop];
Try setting the corner radius of the layer of the button.
[button.layer setCornerRadius:10];
Remember to import if you are using layer property
Also, use:
[[button layer] setMasksToBounds:YES];
With this code the layer gets a corner radius of 10.0 and the -setMasksToBounds: tells the button’s layer to mask any layer content that comes below it in the layer tree. This is necessary in order for the layer to mask off the rounded corners.
UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Change this to:
UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeCustom];
Edited:
UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeCustom];
[[meerKnop layer] setCornerRadius:8.0f];
meerKnop.backgroundColor = [UIColor redcolor];
meerKnop.frame = CGRectMake(11.0, (60.0 + (teller * 52.5)), 299.0, 50.0);
show me where the big white rectangle is appearing? (I hope u have cleared the cell background color).
change the background colour of the button to same as that of cell.. it will take care of the rounded edges as there colour will become same as that of the background. sry i new.. i can think of this way only at the moment.. Hope it helps..

How to use UIView's contentStretch

I want to use an image of size 27x27 with the center 1 pixel stretchable as the backgroundImage of an UIButton. Here is my code:
[button setBackgroundImage:[UIImage imageNamed:#"button_image"] forState:UIControlStateNormal];
button.frame = CGRectMake(0.f, 0.f, 27.f, 27.f);
button.contentStretch = CGRectMake(13.f/27.f, 13.f/27.f, 1.f/27.f, 1.f/27.f);
[button setTitle:NSLocalizedString(#"long title", #"long title") forState:UIControlStateNormal];
[button sizeToFit];
CGRect buttonFrame = button.frame;
buttonFrame.size.width += 18.f * 2;
button.frame = buttonFrame;
But the result is no different from that without setting contentStretch at all.
So how to correctly use contentStretch to achieve what I want?
Please don't tell me again and again that I can achieve the same effect with stretchableImageWithLeftCapWidth:topCapHeight:. I know that and it's apparently not what I'm asking for. I just want to know how to properly use contentStretch. Thanks.
One issue I see is that you're dividing using integer division. 1 / 27 is equal to 0. 1.0 / 27.0 is equal to 0.037...
If all you want is a stretchable background on your button, check out stretchableImageWithLeftCapWidth:topCapHeight: on UIImage. Create the stretchable image that way, then set it to the background of your button.
I don't know the special behaviour with UIButtons (I'd also suggest to use stretchableImages there as it is a compound view). However, if you want to use contentStretch you could do so on an UIView given your data this was:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
view.layer.contents = (id)[[UIImage imageNamed:#"button_image"] CGImage];
view.contentStretch = CGRectMake(13./27., 13./27., 1./27., 1./27.);
This should be enough to have a UIView with a stretchable background.
Use -[UIImage stretchableImageWithLeftCapWidth:topCapHeight:].
I put this in the init method of a subclass of UIView:
// Add errorBackground & errorLabel.
rect.size.height = 26.0f;
errorBackground = [[UIImageView alloc] initWithFrame:rect];
errorBackground.image = [[UIImage imageNamed:#"ErrorBubble.png"]
stretchableImageWithLeftCapWidth:13 topCapHeight:0];
UILabel *errorLabel = [[UILabel alloc] initWithFrame:
CGRectMake(0.0f, 0.0f, rect.size.width, rect.size.height)];
errorLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
errorLabel.backgroundColor = [UIColor clearColor];
errorLabel.textAlignment = UITextAlignmentCenter;
errorLabel.font = [UIFont systemFontOfSize:14.0f];
errorLabel.text = #"1 Failed Passcode Attempt";
errorLabel.textColor = [UIColor whiteColor];
errorLabel.shadowColor = [UIColor blackColor];
errorLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
[errorBackground addSubview:errorLabel];
[errorLabel release];
[self addSubview:errorBackground];
ErrorBubble.png:
ErrorBubble#2x.png:
Unsatisfying but simple answer:
You can't, until Apple fixes this. IMO it's a long outstanding and very annoying bug.
The UIButton implementation consists of a couple of subviews, and the (internal) background view does not seem to reflect the settings done on the UIButton view.

IPhone Text Glow Effect

In my IPhone application, I want the text in UILabel to glow for a second, then fade for a sec;. Also i want to repeat this cycle for say 3 or 4 times.
Is this possible?
As of 3.2 you there is direct support for shadows in the SDK.
label.layer.shadowColor = [label.textColor CGColor];
label.layer.shadowOffset = CGSizeMake(0.0, 0.0);
Play with the parameters:
label.layer.shadowRadius = 3.0;
label.layer.shadowOpacity = 0.5;
And to avoid shadow being clipped by the label bouds:
label.layer.masksToBounds = NO;
Don't forget to
#include <Quartzcore/Quartzcore.h>
and link against the QuartzCore or CoreGraphics frameworks (thanks to commenters for pointing this out).
I've posted some sample code which subclasses UILabel and enables you to apply glow and soft shadows to text.
http://www.redrobotstudios.com/blog/2010/04/29/create-glow-soft-shadow-text-on-iphone/
Yes. Use beginAnimation...commitAnimation, and use the alpha value to brighten or dim the ULabel. Make sure that the default value of the UILabel's alpha starts at 0.85 and brightens to 1.0 and then dims to 0.75, and when all is done, you go back to 0.85.
There are other ways to do it such as having another view on top of the label that is gray or black and you use the same begin...commitAnimation to change the alpha on that from 0 to 0.20 or so.
There are many ways to do this, with varying quality. One way would be to subclass UILabel, and implement some kind of gradient effect in coregraphics in the drawRect method.
You can also play with the text shadow (change the colour and alpha) and see if you can come up with a decent glow.
The easiest way is probably to make a transparent glow-outline image in photoshop and place it behind your text, and then do like mahboudz suggests... fade the image in and out using coreanimation.
- (UILabel *) setUpGlowLabelWithFrame: (CGRect) frame fontSize: (int)fontSize {
UILabel* label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:fontSize];
label.textColor = [UIColor whiteColor];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
label.textAlignment = UITextAlignmentCenter;
label.layer.shadowColor = [label.textColor CGColor];
label.layer.shadowOffset = CGSizeMake(0.0, 0.0);
label.layer.masksToBounds = NO;
label.layer.shadowRadius = 0.5f;
label.layer.shadowOpacity = 0.95;
label.numberOfLines = 2;
label.tag = 20;
return label;
}
I get the glow effect when using this.
Hope it helps.
Happy coding :)
For those of you using Swift 4, here's what I used for multiple objects to Glow the same color as they are:
let colorRed: UIColor? = timeLabel.textColor
timeLabel.layer.shadowColor = colorRed?.cgColor
timeLabel.layer.shadowRadius = 4.0
timeLabel.layer.shadowOpacity = 0.9
timeLabel.layer.shadowOffset = CGSize.zero
timeLabel.layer.masksToBounds = false
As for animating the glow, just add a timer for 3-4 loops and change .shadowOpacity to something lower.