UIView模糊边框

时间:2012-09-10 15:08:36

标签: objective-c ios core-graphics

有没有办法模糊/淡出UIView的边框?

到目前为止,我对核心图形做了很少甚至没有。

2 个答案:

答案 0 :(得分:15)

您可以尝试设置具有较大阴影半径和透明度的UIView的CALayer。类似的东西:

    #include <QuartzCore/QuartzCore.h>

...

    CALayer *layer = myView.layer;
    layer.shadowOpacity = .5;
    layer.shadowColor = [[UIColor blackColor] CGColor];
    layer.shadowOffset = CGSizeMake(0,0);
    layer.shadowRadius = 10;

答案 1 :(得分:1)

使用swift版本添加示例:

    btnSignIn.layer.cornerRadius = btnSignIn.frame.height * 0.5
    btnSignIn.layer.shadowColor = UIColor.darkGray.cgColor
    btnSignIn.layer.shadowRadius = 4.0
    btnSignIn.layer.shadowOpacity = 1.0
    btnSignIn.layer.shadowOffset = CGSize(width: 0, height: 0)

结果:

enter image description here

请记住,如果更改ib编辑器中的阴影设置,它将更改文本的阴影,而不是视图的边框。

您也可以在IB Builder中创建一个类并进行设置:

class UIRoundedWhiteButton: UIButton {

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    layer.cornerRadius = frame.height * 0.5
    layer.shadowColor = UIColor.darkGray.cgColor
    layer.shadowRadius = 4.0
    layer.shadowOpacity = 1.0
    layer.shadowOffset = CGSize(width: 0, height: 0)
}...