如何在iOS中打起阴影? [英] How to round a shadow in iOS?

查看:66
本文介绍了如何在iOS中打起阴影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个圆形的UIView,它也应该有一个阴影.到目前为止,一切正常,但是在拐角处,阴影未正确取整.

I've created a rounded UIView, which should also have a shadow. So far, everything works, but on the corners, the shadow isn't rounded properly.

如何绕过阴影?

这是代码和屏幕截图:

popupView.layer.cornerRadius = 15

popupView.layer.shadowColor = UIColor.black.cgColor
popupView.layer.shadowOffset = CGSize(width: 0, height: 0)
popupView.layer.shadowOpacity = 0.3
popupView.layer.shadowRadius = 3.0
popupView.layer.shadowPath = UIBezierPath(rect: popupView.bounds).cgPath
popupView.layer.shouldRasterize = false

推荐答案

因此,我基本上将您的代码放入Playground并进行了一些尝试,发现我基本上可以删除 view.layer.shadowPath,它将起作用...

So, I basically threw your code into Playground and played around with a bit, what I found was, I could basically remove view.layer.shadowPath and it would work...

import UIKit

let view = UIView(frame: CGRect(x: 100, y: 100, width: 500, height: 500))
view.backgroundColor = UIColor.white
view.layer.cornerRadius = 15

view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOffset = CGSize(width: 0, height: 0)
view.layer.shadowOpacity = 1.0
view.layer.shadowRadius = 6.0
//view.layer.shadowPath = UIBezierPath(rect: view.bounds).cgPath
view.layer.shouldRasterize = false


let outter = UIView(frame: CGRect(x: 0, y: 0, width: 700, height: 700))
outter.backgroundColor = UIColor.red
outter.addSubview(view)


outter

我还发现是:

  • view.layer.masksToBounds = true 没有达到预期的结果
  • view.layer.shadowPath = UIBezierPath(roundedRect:view.bounds,cornerRadius:15).cgPath 也可以使用,但是会增加一个故障点,因为您需要记住设置如果您在视图上更改了 cornerRadius 属性,或者添加了一个变量作为两者的种子,但是由于没有设置 shadowPath ,它将产生相同的结果,这让我想知道为什么你们都要使用它.
  • view.layer.masksToBounds = true did not achieve the desired result
  • view.layer.shadowPath = UIBezierPath(roundedRect: view.bounds, cornerRadius: 15).cgPath also works, but adds in an additional point of failure, as you need to remember to set the cornerRadius property if you change it on the view, or add a variable to seed both, but since if you didn't set the shadowPath, it would generate the same result, it makes me wonder why you'd both using it.

这篇关于如何在iOS中打起阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆