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

标签 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;

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

最佳答案

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

检查这个

关于ios - 如何创建 UIButton 阴影发光效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20656401/

相关文章:

iphone - 为什么人们总是在 Objective-C(即 iPhone)中对实例变量使用重新分配?

iphone - 将 float 转换为整数

objective-c - NSFetchRequest setPropertiesToGroupBy : return all entities that match? 是否

ios - 我一整天都在尝试计算用户字符并将它们打印到屏幕上

ios - 我需要帮助“使用文本字段自动完成 google places( objective-c )

iphone - sizeWithFont : method 的替代方案

objective-c - 在 NSWindows 中双击?

iphone - 如何去除tableview中的白色背景

ios - Xcode 10 Beta 6 Playground 中未调用 Deinit 方法

ios - Starling AS3 : A lot of textures, 内存警告 iOS