我需要在视图的左下角和右下角四舍五入,并根据我在Internet上在UIview扩展内创建此函数的结果:
func setRoundedCorner(withRadius radius: CGSize, forCorners corners: UIRectCorner ) {
let viewShape = CAShapeLayer()
viewShape.bounds = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.bounds.width, height: self.bounds.height)
viewShape.position = self.center
viewShape.path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: radius).cgPath
self.layer.mask = viewShape
}
现在,此解决方案遇到了两个问题;尽管它非常适合静态视图,但我的视图以编程方式更改其高度,导致视图被截断;我找到了解决该问题的解决方法,每次使用setRoundedCorner
方法时都会想起,但是在我看来,这非常不舒服;还有其他解决方法吗?我很难解决的另一个问题是阴影设置。我通常使用这种方法:
func setShadow(height: CGFloat, width: CGFloat, opacity: Float, radius: CGFloat) {
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOpacity = opacity
self.layer.shadowOffset = CGSize(width: width, height: height)
self.layer.cornerRadius = radius
self.layer.masksToBounds = false
}
相反,视图在其下应具有阴影时会以这种方式出现:
但是在这种情况下,似乎根本不显示任何阴影似乎不起作用。谁能给我一些建议?
答案 0 :(得分:1)
在这里,我与您分享我的类别(添加边角半径)(使用BezierPath),同时支持添加阴影。
import UIKit
/// A category for all views.
extension UIView {
/// Setups the layer of a view.
public func setupLayer(
cornerRadius: CGFloat = 22,
borderWidth: CGFloat = 0,
borderColor: UIColor = .clear,
shadowOffSet: CGSize = CGSize(width: 0, height: 1),
shadowColor: UIColor = UIColor(red:0, green:0, blue:0, alpha:0.15),
shadowOpacity: Float = 1,
shadowRadius: CGFloat = 2,
shouldClipToBounds: Bool = false
) {
self.layer.cornerRadius = cornerRadius
self.layer.borderWidth = borderWidth
self.layer.borderColor = borderColor.cgColor
self.layer.shadowOffset = shadowOffSet
self.layer.shadowColor = shadowColor.cgColor
self.layer.shadowOpacity = shadowOpacity
self.layer.shadowRadius = shadowRadius
self.clipsToBounds = shouldClipToBounds
}
/// Round the corner radius with bezier path.
func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
随时编辑所有内容。现在,让我们谈谈您的问题。顺便说一下,我在生产项目中使用了此代码。有时,您需要在viewDidLayoutSubviews
,viewWillLayoutSubviews
中四舍五入。而且,如果您使用的是UIView
或UITableViewCell
或类似类的子类,则可以在其draw
函数内添加圆角代码。
override func draw(_ rect: CGRect) {
super.draw(rect)
// Round corners here...
}
最后,为了设置图层,我通常在控件的惰性初始化声明(例如按钮,textFields ...)中调用它。知道使用bezierPath和简单的layer.cornerRadius添加角的区别。 :)
我希望这会有所帮助!
答案 1 :(得分:0)
您可以简单地添加此扩展名
extension UIView
{
func dropShadow(scale: Bool = true) {
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.5
layer.shadowOffset = CGSize(width: -1, height: 1)
layer.shadowRadius = 1
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
func dropShadow(color: UIColor, opacity: Float = 0.5, offSet: CGSize, radius: CGFloat = 1, scale: Bool = true) {
layer.masksToBounds = false
layer.shadowColor = color.cgColor
layer.shadowOpacity = opacity
layer.shadowOffset = offSet
layer.shadowRadius = radius
layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
}
添加上述扩展名后,您可以像以下使用
priceview.layer.cornerRadius = 5
priceview.dropShadow(color: UIColor.darkGray, opacity: 0.5, offSet: CGSize(width: 1, height: 1), radius: 3, scale: true)
希望它能对您有所帮助。