Swift 在与视图具有相同角半径的UIView上应用阴影
您好,我有一些圆角视图,我想对这些视图应用阴影 因此,首先我要谈谈这个观点:Swift 在与视图具有相同角半径的UIView上应用阴影,swift,xcode,uikit,Swift,Xcode,Uikit,您好,我有一些圆角视图,我想对这些视图应用阴影 因此,首先我要谈谈这个观点: view.layer.cornerRadius = 15 view.clipsToBounds = true 然后我应用阴影: func dropShadow(scale: Bool = true) { self.layer.masksToBounds = false self.layer.cornerRadius = 15 self.layer.shadowC
view.layer.cornerRadius = 15
view.clipsToBounds = true
然后我应用阴影:
func dropShadow(scale: Bool = true) {
self.layer.masksToBounds = false
self.layer.cornerRadius = 15
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath
self.layer.shadowOffset = CGSize(width: 3, height: 3)
self.layer.shadowOpacity = 0.5
self.layer.shadowRadius = 5
}
我得到了带阴影的圆形视图,但阴影不像我的视图那样圆形。阴影一点也不圆角不建议尝试使用一个视图句柄来处理圆角和阴影逻辑。创建所需效果的最佳方法是使用另一个拥有阴影的父视图包装圆角视图 这可以用操场来最好地说明。在操场上试试这个代码
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let shadowView = UIView()
shadowView.frame = CGRect(x: 50, y: 200, width: 200, height: 100)
shadowView.backgroundColor = nil
shadowView.layer.shadowColor = UIColor.black.cgColor
shadowView.layer.shadowOffset = CGSize(width: 0, height: 4.0)
shadowView.layer.shadowRadius = 8.0
shadowView.layer.shadowOpacity = 1
let roundedView = UIView()
roundedView.frame = shadowView.bounds
roundedView.backgroundColor = .gray
roundedView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner,.layerMaxXMinYCorner]
roundedView.layer.cornerRadius = 10
roundedView.layer.masksToBounds = true
shadowView.addSubview(roundedView)
view.addSubview(shadowView)
view.backgroundColor = UIColor.white
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
不要忘记在
viewWillLayoutSubviews
或layoutSubviews
中设置阴影视图图层的shadowPath
属性。如果此属性设置为nil
,UIKit必须执行屏幕外渲染以确定如何绘制阴影矩形。您不能从其clipsToBounds
为true
的视图投射阴影。如果视图的masksToBounds
为true
,则其clipsToBounds
为true
;它们是一样的
如果希望阴影来自剪辑的视图,则需要使用两个视图:一个是用户可以看到的视图,圆角和clipstobunds
设置为true
,另一个是用户看不到的视图,因为它位于第一个视图的后面,也有圆角,但是将clipstobunds
设置为false
,可以投射阴影
请注意,两个视图都不是另一个视图的子视图,也没有进行剪辑的超级视图,因为这样会剪辑阴影。能否显示阴影的屏幕截图?链接到屏幕截图,@Sweeper,看起来你的视图是另一个视图的子视图,它覆盖了阴影。使阴影半径变小,并在视图与其父视图之间添加一些空间。另外,请尝试不要设置阴影路径,这会导致阴影路径使用默认路径。我在3个视图上遇到问题,请查看该图像上的情节提要:请给我可以直接在视图上使用的代码,如果可能,请提供UIView扩展。我试过你的游乐场,但什么也没发生。我试过:``在self.views{view!.layer.cornerRadius=15 view!.clipsToBounds=false view!.layer.masksToBounds=false view!.layer.shadowColor=UIColor.black.cgColor view!.layer.shadowPath=UIBezierPath中查看(roundedRect:view!.bounds,cornerRadius:view!.layer.cornerRadius).cgPath视图!.layer.shadowOffset=CGSize(宽度:3,高度:3)视图!.layer.shadowOpacity=0.5视图!.layer.shadowRadius=5}“``还是不圆的阴影耸耸肩。我给你看了我全部的代码,我给你看了结果。如果你想得到我得到的结果,就按我做的去做。你在做别的事情,所以你得到了一个不同的结果。你的阴影是圆的。但那是因为你没有听我在回答的最后一段中说的话。问题不在于您的阴影舍入;问题是阴影视图的superview将阴影限制为其自身的矩形。您告诉我如果clipsTobounds设置为false,我可以看到圆形阴影。这就是我所做的,但仍然没有舍入。我不使用direct而使用storyboard,这是我从您的代码中得到的:
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let shadowView = UIView()
shadowView.frame = CGRect(x: 50, y: 200, width: 200, height: 100)
shadowView.backgroundColor = nil
shadowView.layer.shadowColor = UIColor.black.cgColor
shadowView.layer.shadowOffset = CGSize(width: 0, height: 4.0)
shadowView.layer.shadowRadius = 8.0
shadowView.layer.shadowOpacity = 1
let roundedView = UIView()
roundedView.frame = shadowView.bounds
roundedView.backgroundColor = .gray
roundedView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner,.layerMaxXMinYCorner]
roundedView.layer.cornerRadius = 10
roundedView.layer.masksToBounds = true
shadowView.addSubview(roundedView)
view.addSubview(shadowView)
view.backgroundColor = UIColor.white
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
class ShadowView : UIView {
override init(frame: CGRect) {
super.init(frame:frame)
self.isOpaque = true
self.backgroundColor = .black
self.dropShadow()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func dropShadow() {
self.layer.masksToBounds = false
self.layer.cornerRadius = 15
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 3, height: 3)
self.layer.shadowOpacity = 0.5
self.layer.shadowRadius = 5
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let r = CGRect(x: 100, y: 100, width: 100, height: 100)
let v = UIImageView(frame:r)
v.image = UIImage(named:"marsSurface.jpg")
v.clipsToBounds = true
v.backgroundColor = .red
v.layer.cornerRadius = 15
self.view.addSubview(ShadowView(frame:r))
self.view.addSubview(v)
}
}