Чтобы придать угловой радиус только верхней части представления в Swift, вы можете использовать несколько методов. Вот некоторые часто используемые подходы:
Метод 1: использование UIBezierPathи CAShapeLayer
let cornerRadius: CGFloat = 10.0
let maskPath = UIBezierPath(roundedRect: yourView.bounds,
byRoundingCorners: [.topLeft, .topRight],
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
let shape = CAShapeLayer()
shape.path = maskPath.cgPath
yourView.layer.mask = shape
Метод 2: использование CABasicAnimationи CATransaction
let cornerRadius: CGFloat = 10.0
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: yourView.bounds,
byRoundingCorners: [.topLeft, .topRight],
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)).cgPath
yourView.layer.mask = maskLayer
let radiiAnimation = CABasicAnimation(keyPath: "path")
radiiAnimation.fromValue = yourView.layer.mask?.path
radiiAnimation.toValue = maskLayer.path
radiiAnimation.duration = 0.3
CATransaction.begin()
yourView.layer.mask?.path = maskLayer.path
CATransaction.commit()
Метод 3: использование расширения UIView
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let maskPath = UIBezierPath(roundedRect: bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = bounds
maskLayer.path = maskPath.cgPath
layer.mask = maskLayer
}
}
// Usage:
yourView.roundCorners(corners: [.topLeft, .topRight], radius: 10.0)