ios – 如何控制阴影传播和模糊?

前端之家收集整理的这篇文章主要介绍了ios – 如何控制阴影传播和模糊?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经在草图中设计了UI元素,其中一个具有模糊1和展开0.的阴影.我查看了视图层属性的文档,层没有任何名为spread或blur或任何等同物(唯一的控件是只是shadowOpacity)如何控制像模糊和传播的东西? @H_301_2@编辑:

@H_301_2@以下是我在Sketch中的设置:

@H_301_2@这就是我希望我的阴影看起来像:

@H_301_2@

@H_301_2@这里现在是什么样子的:

注意,你必须点击图片实际看到阴影.

@H_301_2@我的代码如下:

func setupLayer(){
    view.layer.cornerRadius = 2
    view.layer.shadowColor = Colors.Shadow.CGColor
    view.layer.shadowOffset = CGSize(width: 0,height: 1)
    view.layer.shadowOpacity = 0.9
    view.layer.shadowRadius = 5
}

解决方法

你可以试试这个….你可以玩这些价值观.
shadowRadius指定模糊量. shadowOffset指示阴影在哪里.
@H_301_2@Swift 2.0

let radius: CGFloat = demoView.frame.width / 2.0 //change it to .height if you need spread for height
let shadowPath = UIBezierPath(rect: CGRect(x: 0,y: 0,width: 2.1 * radius,height: demoView.frame.height))
//Change 2.1 to amount of spread you need and for height replace the code for height

demoView.layer.cornerRadius = 2
demoView.layer.shadowColor = UIColor.blackColor().CGColor
demoView.layer.shadowOffset = CGSize(width: 0.5,height: 0.4)  //Here you control x and y
demoView.layer.shadowOpacity = 0.5
demoView.layer.shadowRadius = 5.0 //Here your control your blur
demoView.layer.masksToBounds =  false
demoView.layer.shadowPath = shadowPath.CGPath
@H_301_2@Swift 3.0

let radius: CGFloat = demoView.frame.width / 2.0 //change it to .height if you need spread for height 
let shadowPath = UIBezierPath(rect: CGRect(x: 0,height: demoView.frame.height)) 
//Change 2.1 to amount of spread you need and for height replace the code for height

demoView.layer.cornerRadius = 2
demoView.layer.shadowColor = UIColor.black.cgColor
demoView.layer.shadowOffset = CGSize(width: 0.5,height: 0.4)  //Here you control x and y
demoView.layer.shadowOpacity = 0.5
demoView.layer.shadowRadius = 5.0 //Here your control your blur
demoView.layer.masksToBounds =  false
demoView.layer.shadowPath = shadowPath.cgPath
@H_301_2@Example with spread

@H_301_2@

@H_301_2@To create a basic shadow

demoView.layer.cornerRadius = 2
    demoView.layer.shadowColor = UIColor.blackColor().CGColor
    demoView.layer.shadowOffset = CGSizeMake(0.5,4.0); //Here your control your spread
    demoView.layer.shadowOpacity = 0.5 
    demoView.layer.shadowRadius = 5.0 //Here your control your blur
@H_301_2@Basic Shadow example in Swift 2.0

@H_301_2@

猜你在找的iOS相关文章