2013-12-18 64 views
1

我創建了一個UIButton,並添加了使用CAShapeLayer和UIBezierPath來製作按鈕圓的自定義圖層。如何創建UIButton陰影發光效果?

在按鈕水龍頭功能我添加了發光效果陰影,但它不能工作。

按鈕創建代碼:

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

遺憾的是沒有出現光暈效果:(

+0

煥發這裏意味着你要高亮但是噸? –

+0

是的,我想陰影圍繞按鈕的發光效果 – RakeshR

+0

@RakeshR _closeButton.showsTouchWhenHighlighted = YES;試試這一個 –

回答

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

檢查這個