如何在 Swift 3 中的 TableView 单元格内设置 .top right 上角半径和更高的值 t
How to set .topright to corner redius with higher value t inside TableView Cell in Swift 3
我想为 TableView Cell 中的 UIView 设置不同的角半径值和阴影效果,如下所示:
我的代码是:
cell.headerView.round(corners: [.topRight], radius: 35)
extension UIView {
func round(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
self.layoutIfNeeded()
}
}
这在 iPhone SE 模拟器中有效,但在 iPhone 6S 模拟器中无效。
Swift 4
myView.clipsToBounds = true
myView.layer.cornerRadius = 20
myView.layer.maskedCorners = [.layerMaxXMaxYCorner,.layerMinXMaxYCorner,.layerMinXMinYCorner]
我使用了几乎相同的实现,它在设备和模拟器上都运行良好。
extension UIView
{
func roundCorners(corners: UIRectCorner, radius: CGFloat)
{
let bounds = self.bounds
let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = bounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
}
}
class CustomCell: UITableViewCell
{
@IBOutlet weak var customView: UIView!
override func awakeFromNib()
{
super.awakeFromNib()
self.customView.layer.cornerRadius = 10.0
self.customView.roundCorners(corners: [.topRight], radius: 35.0)
}
}
截图:
我想为 TableView Cell 中的 UIView 设置不同的角半径值和阴影效果,如下所示:
我的代码是:
cell.headerView.round(corners: [.topRight], radius: 35)
extension UIView {
func round(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
self.layoutIfNeeded()
}
}
这在 iPhone SE 模拟器中有效,但在 iPhone 6S 模拟器中无效。
Swift 4
myView.clipsToBounds = true
myView.layer.cornerRadius = 20
myView.layer.maskedCorners = [.layerMaxXMaxYCorner,.layerMinXMaxYCorner,.layerMinXMinYCorner]
我使用了几乎相同的实现,它在设备和模拟器上都运行良好。
extension UIView
{
func roundCorners(corners: UIRectCorner, radius: CGFloat)
{
let bounds = self.bounds
let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = bounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
}
}
class CustomCell: UITableViewCell
{
@IBOutlet weak var customView: UIView!
override func awakeFromNib()
{
super.awakeFromNib()
self.customView.layer.cornerRadius = 10.0
self.customView.roundCorners(corners: [.topRight], radius: 35.0)
}
}
截图: