【发布时间】:2020-09-18 16:21:23
【问题描述】:
我正在尝试设置一些 CALayers 的框架,但是当我运行此代码时,它们似乎向右移动。
框架坐标和阴影是正确的。 我将此类用作故事板中视图的 UIView 类。
class Shadows: UIView {
let darkShadow = CALayer()
let lightShadow = CALayer()
override init(frame: CGRect) {
super.init(frame: frame)
}
override func layoutSublayers(of layer: CALayer) {
super.layoutSublayers(of: layer)
shadows()
}
private func shadows() {
let cornerRadius: CGFloat = 30
let shadowRadius: CGFloat = 8
layer.cornerRadius = cornerRadius
layer.masksToBounds = false
darkShadow.frame = frame
darkShadow.backgroundColor = UIColor.white.cgColor
darkShadow.shadowColor = UIColor.black.cgColor
darkShadow.cornerRadius = cornerRadius
darkShadow.shadowOffset = CGSize(width: shadowRadius, height: shadowRadius)
darkShadow.shadowOpacity = 0.2
darkShadow.shadowRadius = shadowRadius
layer.insertSublayer(darkShadow, at: 0)
lightShadow.frame = frame
lightShadow.backgroundColor = UIColor.white.cgColor
lightShadow.shadowColor = UIColor.white.cgColor
lightShadow.cornerRadius = cornerRadius
lightShadow.shadowOffset = CGSize(width: -shadowRadius, height: -shadowRadius)
lightShadow.shadowOpacity = 0.7
lightShadow.shadowRadius = shadowRadius
layer.insertSublayer(lightShadow, at: 0)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
【问题讨论】:
标签: ios swift xcode calayer shadow