Friday, July 3, 2026
HomeiOS Developmentios - Learn how to use lineJoin and lineJoinStyle with a UIBezierPath?

ios – Learn how to use lineJoin and lineJoinStyle with a UIBezierPath?

[ad_1]

I’ve a masks utilized to a view utilizing CAShapeLayer and UIBezierPath. I would like so as to add a rounding impact to the road joins nevertheless it’s not working. How do I around the corners of this form?

You may plug the next into an Xcode playground.

import PlaygroundSupport
import UIKit

non-public class ProfileImageView: UIView {
    non-public let imageView = UIImageView()
    var picture: UIImage?
    
    override init(body: CGRect) {
        tremendous.init(body: body)
        imageView.clipsToBounds = true
        imageView.backgroundColor = UIColor.black
        imageView.contentMode = .scaleAspectFill
        imageView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(imageView)
        imageView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        imageView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        imageView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        imageView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
    }
    
    required init?(coder: NSCoder) {
        return nil
    }
    
    override func draw(_ rect: CGRect) {
        let h = rect.top
        let w = rect.width
        let path = UIBezierPath()
        let shapeLayer = CAShapeLayer()
        
        path.transfer(to: .zero)
        path.addLine(to: CGPoint(x: w-32, y: 0))
        path.addLine(to: CGPoint(x: w, y: 32))
        path.addLine(to: CGPoint(x: w, y: h))
        path.addLine(to: CGPoint(x: 32, y: h))
        path.addLine(to: CGPoint(x: 0, y: h-32))
        path.shut()
        path.lineJoinStyle = .spherical
        shapeLayer.lineJoin = .spherical
        shapeLayer.path = path.cgPath
        layer.masks = shapeLayer
        imageView.picture = picture
    }
}

class VC: UIViewController {
    override func loadView() {
        view = UIView()
        view.backgroundColor = .grey
        
        let imgView = ProfileImageView()
        imgView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(imgView)
        imgView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        imgView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        imgView.widthAnchor.constraint(equalTo: view.widthAnchor, fixed: -64).isActive = true
        imgView.heightAnchor.constraint(equalTo: view.widthAnchor, fixed: -64).isActive = true
    }
}

PlaygroundPage.present.liveView = VC()

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments