RakeshR RakeshR - 24 days ago 8
Objective-C Question

how to create UIButton shadow glow effect?

i created a UIButton and added custom layer using CAShapeLayer and UIBezierPath used to make button circle .

In button tap function i added a glow effect shadow but it can't works.

Button creation code :

_closeButton = [UIButton buttonWithType:UIButtonTypeCustom];

_closeButton.frame = CGRectMake(6.0, 6.0, 20.0, 20.0);

CAShapeLayer *circleLayer = [CAShapeLayer layer];

[circleLayer setBounds:CGRectMake(0.0f, 0.0f, [_closeButton bounds].size.width,
[_closeButton bounds].size.height)];

[circleLayer setPosition:CGPointMake(CGRectGetMidX([_closeButton bounds]),CGRectGetMidY([_closeButton bounds]))];

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, CGRectGetWidth(_closeButton.frame), CGRectGetHeight(_closeButton.frame))];

[circleLayer setPath:[path CGPath]];

[circleLayer setStrokeColor:[[UIColor whiteColor] CGColor]];

[circleLayer setLineWidth:2.50f];

[circleLayer setFillColor:[[UIColor blackColor] CGColor]];

[[_closeButton layer] addSublayer:circleLayer];

_closeButton.layer.cornerRadius = _closeButton.bounds.size.width / 2.0;

[_closeButton setTitle:@"X" forState:UIControlStateNormal];

_closeButton.titleLabel.textAlignment = NSTextAlignmentCenter;

[_closeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0]];

[_closeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[_closeButton addTarget:self action:@selector(closeButtonPressed)

forControlEvents:UIControlEventTouchUpInside];

_qbWebView.scalesPageToFit = YES;

_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

_activityIndicator.hidesWhenStopped = YES;

[_qbView addSubview:_qbWebView];

[_qbView addSubview:_closeButton];


Button tap function

_closeButton.layer.shadowColor = [[UIColor whiteColor] CGColor];

_closeButton.layer.shadowRadius = 40.0f;

_closeButton.layer.shadowOpacity = .9;

_closeButton.layer.shadowOffset = CGSizeMake(40.0f, 40.0f);

_closeButton.layer.masksToBounds = NO;


Unfortunately no glow effect appears :(

Answer Source
_closeButton.imageView.layer.cornerRadius = 7.0f; 
_closeButton.layer.shadowRadius = 3.0f; 
_closeButton.layer.shadowColor = [UIColor blackColor].CGColor;
_closeButton.layer.shadowOffset = CGSizeMake(0.0f, 1.0f); 
_closeButton.layer.shadowOpacity = 0.5f;
_closeButton.layer.masksToBounds = NO; 

check this