swift - Swift 阴影没有正确传播
问题描述
我一直在尝试为我的 UIView 创建一个阴影。我环顾四周,从这篇文章中找到了 CALayer 类的扩展。https://stackoverflow.com/a/48489506/9188318
到目前为止,它对我来说一直运作良好,直到我尝试为点差输入一个非 0 数字。
点差为 0。这是结果
这是使用传播 1 的结果
传播 5 时情况变得更糟
我遇到的问题是它没有圆角,我不知道如何解决这个问题。这是我使用此视图的 UIView 的代码。用于制作阴影的扩展在上面的帖子中。
UIView 代码
class FileCalculateOperatorSelectionButton : UIView {
private var isActivated : Bool = false
//Background colors
private var unSelectedBackgroundColor : UIColor = UIColor(red: 178/255, green: 90/255, blue: 253/255, alpha: 1.0)
private var selectedBackgroundColor : UIColor = UIColor.white
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.commonInit()
}
private func commonInit() {
self.layer.cornerRadius = self.frame.height/2
self.backgroundColor = self.unSelectedBackgroundColor
let shadowColor = UIColor(red: 0xC8, green: 0xC6, blue: 0xC6)
//Change the spread argument here
self.layer.applySketchShadow(color: .black, alpha: 0.5, x: 0, y: 0, blur: 5, spread: 0)
}
}
解决方案
尝试将layer's
masksToBounds
属性设置为true
.
self.layer.masksToBounds = true
推荐阅读
- node.js - 在nodeJS中的cookie中存储数组
- python - 在 pyright 中修复“无法解析导入 [模块]”
- c# - 如何有选择地从文件中复制详细信息并将其粘贴到新文件中?
- python - PySide2 Qt 表面示例
- java - 优化这种从基础到基础的数据比较的想法?
- awk - 在命令行中运行 awk 程序时出错:“^ 反斜杠不是最后一个字符”
- angular2-nativescript - 迁移到 NativeScript 6.0 后在 Android 上运行的问题
- javascript - 对数组方法中的“thisArg”感到困惑
- django - Django 2.2:如何将用户从视图重定向到具有上下文的特定页面?
- laravel - Laravel 上的多个 SESSION_DOMAIN