1.加阴影导致卡顿
let footView = UIView()
footView.layer.shadowRadius = 1
footView.layer.shadowOffset = CGSize(width: 1, height: 1)
footView.layer.shadowOpacity = 0.8
footView.layer.shadowColor = UIColor.black.cgColor
footView.backgroundColor = UIColor.white
上面这张添加阴影的方式只使用于添加少量阴影,如果添加大量阴影的话,会造成大量的离屏渲染,严重影响性能,从而造成卡顿。解决方式就是为阴影指定路径(具体原理大家可以去百度):
let footView = UIView()
footView.layer.shadowPath = UIBezierPath(rect: footView.bounds).cgPath //指定阴影的路径
footView.layer.shadowRadius = 1
footView.layer.shadowOffset = CGSize(width: 1, height: 1)
footView.layer.shadowOpacity = 0.8
footView.layer.shadowColor = UIColor.black.cgColor
footView.backgroundColor = UIColor.white
2.添加阴影无法显示
阴影是添加在视图的外部,而不是内部,如果阴影没有显示的空间,就会造成阴影无法显示的假象,所以需要给阴影留出显示的空间,例如上述代码,必须留1的距离让阴影显示。