如何创建UIButton阴影发光效果?

时间:2013-12-18 11:05:31

标签: ios iphone objective-c uibutton

我创建了一个UIButton,并使用CAShapeLayer和UIBezierPath添加了自定义图层,用于制作按钮圈。

在按钮点击功能中,我添加了一个发光效果阴影,但它不起作用。

按钮创建代码:

_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];

按钮点击功能

_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;

不幸的是,没有出现发光效果:(

1 个答案:

答案 0 :(得分:2)

_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; 

检查这个