How to give cornerRadius for UIBezierPath
You can use below method to make all corner round of view...
UIBezierPath(roundedRect: anyView.bounds, cornerRadius: CGSize(width: 10.0, height: 10.0))
If you want particular corner to make round use below method.
UIBezierPath(roundedRect: anyView.bounds,
byRoundingCorners: .BottomLeft | .BottomRight,
cornerRadius: CGSize(width: 10.0, height: 10.0))
How to set Corner Radius to UIBezierPath
before your let bezierPath = UIBezierPath() for curve add
var = cornerRadiudSize = CGSize(width: 50.0, height: 50.0) //this is your global variable that you can change based on the slider and then draw
let p = UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: cornerRadiudSize)
p.addClip()
PS: Instead of all corner set the corners you want
How to set cornerRadius for only top-left and top-right corner of a UIView?
Pay attention to the fact that if you have layout constraints attached to it, you must refresh this as follows in your UIView subclass:
override func layoutSubviews() {
super.layoutSubviews()
roundCorners(corners: [.topLeft, .topRight], radius: 3.0)
}
If you don't do that it won't show up.
And to round corners, use the extension:
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
Additional view controller case: Whether you can't or wouldn't want to subclass a view, you can still round a view. Do it from its view controller by overriding the viewWillLayoutSubviews()
function, as follows:
class MyVC: UIViewController {
/// The view to round the top-left and top-right hand corners
let theView: UIView = {
let v = UIView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))
v.backgroundColor = .red
return v
}()
override func loadView() {
super.loadView()
view.addSubview(theView)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// Call the roundCorners() func right there.
theView.roundCorners(corners: [.topLeft, .topRight], radius: 30)
}
}
Set specific value to round corners of UIBezierPath
The cornerRadius
only adjusts the corners of the bounds of the layer, not of the path. If you want path with rounded corners, you’ll have to stroke that yourself.
There are two approaches to rendering an arc with a particular corner radius:
If rendering a simple solid rendition, one very simple approach is to render two arcs, one clockwise, one counter clockwise, using different radii. The line width of these individual arcs should be twice the desired corner rounding of the final shape. Then, if you render these two arcs with a matching stroke color and fill color, you’ll get the desired shape.
Here it is, animated so you can see what’s going on:
This is the code (without the animation):
class ArcView: UIView {
var startAngle: CGFloat = .pi * 3 / 4
var endAngle: CGFloat = .pi * 5 / 4
var clockwise: Bool = true
/// Radius of center of this arc
var radius: CGFloat = 100
/// The linewidth of this thick arc
var lineWidth: CGFloat = 50
/// The corner radius of this thick arc
var cornerRadius: CGFloat = 10
static override var layerClass: AnyClass { return CAShapeLayer.self }
var shapeLayer: CAShapeLayer { return layer as! CAShapeLayer }
override func layoutSubviews() {
super.layoutSubviews()
updatePath()
}
func updatePath() {
let center = CGPoint(x: bounds.midX, y: bounds.midY)
let innerRadius = radius - lineWidth / 2 + cornerRadius
let innerAngularDelta = asin(cornerRadius / innerRadius) * (clockwise ? 1 : -1)
let outerRadius = radius + lineWidth / 2 - cornerRadius
let outerAngularDelta = asin(cornerRadius / outerRadius) * (clockwise ? 1 : -1)
let path = UIBezierPath(arcCenter: center, radius: innerRadius, startAngle: startAngle + innerAngularDelta, endAngle: endAngle - innerAngularDelta, clockwise: clockwise)
path.addArc(withCenter: center, radius: outerRadius, startAngle: endAngle - outerAngularDelta, endAngle: startAngle + outerAngularDelta, clockwise: !clockwise)
path.close()
// configure shapeLayer
shapeLayer.lineWidth = cornerRadius * 2
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.strokeColor = UIColor.blue.cgColor
shapeLayer.lineJoin = .round
shapeLayer.path = path.cgPath
}
}The only trick in the above is how to adjust the starting and ending angles of these inner and outer arcs such that they’ll be perfectly circumscribed by the desired final shape. But a little trigonometry can be used to figure out those angular deltas, as shown above.
The other approach is to define a path for the outline of the desired shape. Calculating the inner arc and outer arcs is similar, but you have to manually calculate the angles for the four rounded corners. It’s just a little trigonometry, but it’s a little hairy:
class ArcView: UIView {
var startAngle: CGFloat = .pi * 3 / 4
var endAngle: CGFloat = .pi * 5 / 4
var clockwise: Bool = true
/// Radius of center of this arc
var radius: CGFloat = 100
/// The linewidth of this thick arc
var lineWidth: CGFloat = 100
/// The corner radius of this thick arc
var cornerRadius: CGFloat = 10
static override var layerClass: AnyClass { return CAShapeLayer.self }
var shapeLayer: CAShapeLayer { return layer as! CAShapeLayer }
override func layoutSubviews() {
super.layoutSubviews()
updatePath()
}
func updatePath() {
let center = CGPoint(x: bounds.midX, y: bounds.midY)
let innerRadius = radius - lineWidth / 2
let innerAngularDelta = asin(cornerRadius / (innerRadius + cornerRadius)) * (clockwise ? 1 : -1)
let outerRadius = radius + lineWidth / 2
let outerAngularDelta = asin(cornerRadius / (outerRadius - cornerRadius)) * (clockwise ? 1 : -1)
let path = UIBezierPath(arcCenter: center, radius: innerRadius, startAngle: startAngle + innerAngularDelta, endAngle: endAngle - innerAngularDelta, clockwise: clockwise)
var angle = endAngle - innerAngularDelta
var cornerStartAngle = angle + .pi * (clockwise ? 1 : -1)
var cornerEndAngle = endAngle + .pi / 2 * (clockwise ? 1 : -1)
var cornerCenter = CGPoint(x: center.x + (innerRadius + cornerRadius) * cos(angle), y: center.y + (innerRadius + cornerRadius) * sin(angle))
path.addArc(withCenter: cornerCenter, radius: cornerRadius, startAngle: cornerStartAngle, endAngle: cornerEndAngle, clockwise: !clockwise)
angle = endAngle - outerAngularDelta
cornerStartAngle = cornerEndAngle
cornerEndAngle = endAngle - outerAngularDelta
cornerCenter = CGPoint(x: center.x + (outerRadius - cornerRadius) * cos(angle), y: center.y + (outerRadius - cornerRadius) * sin(angle))
path.addArc(withCenter: cornerCenter, radius: cornerRadius, startAngle: cornerStartAngle, endAngle: cornerEndAngle, clockwise: !clockwise)
path.addArc(withCenter: center, radius: outerRadius, startAngle: endAngle - outerAngularDelta, endAngle: startAngle + outerAngularDelta, clockwise: !clockwise)
angle = startAngle + outerAngularDelta
cornerStartAngle = angle
cornerEndAngle = startAngle - .pi / 2 * (clockwise ? 1 : -1)
cornerCenter = CGPoint(x: center.x + (outerRadius - cornerRadius) * cos(angle), y: center.y + (outerRadius - cornerRadius) * sin(angle))
path.addArc(withCenter: cornerCenter, radius: cornerRadius, startAngle: cornerStartAngle, endAngle: cornerEndAngle, clockwise: !clockwise)
angle = startAngle + innerAngularDelta
cornerStartAngle = cornerEndAngle
cornerEndAngle = angle + .pi * (clockwise ? 1 : -1)
cornerCenter = CGPoint(x: center.x + (innerRadius + cornerRadius) * cos(angle), y: center.y + (innerRadius + cornerRadius) * sin(angle))
path.addArc(withCenter: cornerCenter, radius: cornerRadius, startAngle: cornerStartAngle, endAngle: cornerEndAngle, clockwise: !clockwise)
path.close()
// configure shapeLayer
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.strokeColor = UIColor.clear.cgColor
shapeLayer.lineJoin = .round
shapeLayer.path = path.cgPath
}
}
How I can rounded corners UIBezierPath?
let cgPath = CGMutablePath()
cgPath.move(to: bottomLeftPoint)
cgPath.addArc(tangent1End: topLeftPoint, tangent2End: topRightPoint, radius: radius)
cgPath.addLine(to: topRightPoint)
let bezierPath = UIBezierPath(cgPath: cgPath)
UIBezierPath - Add rounded corner
Include the following.
circle.lineCap = kCALineJoinRound;
For Swift 3.0 and above
circle.lineCapStyle = .Round;
How to add corner radius to a UIBezier Path Arc
All thats needed is to add one line of code:
path.lineCapStyle = .Round.
How to add rounded corner to a UIBezierPath custom rectangle?
Instead of starting the code with a straight line :
path.moveToPoint(CGPoint(x: 300, y: 0))
I instead start with an arc (upper right):
path.addArcWithCenter(CGPoint(x: 300-10, y: 50), radius: 10 , startAngle: 0 , endAngle: CGFloat(M_PI/2) , clockwise: true) //1st rounded corner
and by doing this, I have four rounded corners and I just need to add a straight line at the end of the code right before:
path.closePath()
Here is the code and a screenshot:
let path = UIBezierPath()
path.addArcWithCenter(CGPoint(x: 300-10, y: 50), radius: 10 , startAngle: 0 , endAngle: CGFloat(M_PI/2) , clockwise: true) //1st rounded corner
path.addArcWithCenter(CGPoint(x: 200, y: 50), radius:10, startAngle: CGFloat(2 * M_PI / 3), endAngle:CGFloat(M_PI) , clockwise: true)// 2rd rounded corner
path.addArcWithCenter(CGPoint(x: 200, y: 10), radius:10, startAngle: CGFloat(M_PI), endAngle:CGFloat(3 * M_PI / 2), clockwise: true)// 3rd rounded corner
// little triangle
path.addLineToPoint(CGPoint(x:240 , y:0))
path.addLineToPoint(CGPoint(x: 245, y: -10))
path.addLineToPoint(CGPoint(x:250, y: 0))
path.addArcWithCenter(CGPoint(x: 290, y: 10), radius: 10, startAngle: CGFloat(3 * M_PI / 2), endAngle: CGFloat(2 * M_PI ), clockwise: true)
path.addLineToPoint(CGPoint(x:300 , y:50))
path.closePath()
Related Topics
Uitapgesturerecognizer Sender Is the Gesture, Not the UI Object
Transition Delegate for Uitabbarcontroller Animation
App Groups Forsecurityapplicationgroupidentifier Returns Nil
How to Call Secitemcopymatching in Xcode 7 Beta 4
How to Add Custom Data to Marker (Google Maps API Swift)
Drawing an Infinite Grid in iOS
How to Set Clear Background in Table View Cell Swipe Action
Value of Type 'Error' Has No Member 'Code'
(Swift Spritekit) Rotate Sprite in the Direction of Touch
Set the Center of a Uibutton Programmatically - Swift
Memory Usage Keeps Rising on Older Devices Using Metal
Setcollectionviewlayout' Animation Broken When Also Changing Collection View Frame
Swift Lazy and Optional Properties