0

I am trying to get random CGPoints along the 'outer contour' of UIView so that I will be able to draw UIBezierPath line over obtained CGPoints. For example, get CGPoints from below red dots which I marked myself by hand. Any idea?

enter image description here

###Edit### As per Sweeper's advice I am trying to add CAShapeLayer into my custom view but it returns nil when I am printing its path. Please could you point out what I am missing?

class ViewController: UIViewController {
    let shapeLayer = CAShapeLayer()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let view = BubbleView(frame: CGRect(x: self.view.frame.midX / 2, y: self.view.frame.midY/2, width: 150, height: 100))
        view.backgroundColor = .gray
        view.layer.cornerRadius = 30
        self.view.addSubview(view)  
    }
}
class BubbleView: UIView {

    var pathLayer: CAShapeLayer!

    override func layoutSubviews() {
        pathLayer = CAShapeLayer()
        pathLayer.bounds = frame
        self.layer.addSublayer(pathLayer)
        print(pathLayer.path) // returns nil
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
Matthew So
  • 75
  • 1
  • 2
  • 8
  • 2
    "so that I will be able to draw UIBezierPath line over it", so I assume you don't have the path that forms the contour? – Sweeper Dec 10 '20 at 09:53
  • @Sweeper Thank you for your comment. For the UIView that I need CGPoints, I have its frame and bounds and I don't have its path, if it is what you asked for. – Matthew So Dec 10 '20 at 10:01
  • 2
    Then the problem might not be as easy as you think (unless you limit it to few types of views). Think about what happens if the view is a `UILabel` with an arbitrary font. Do you want the contours of the glyphs? What if the view is a `UIImageView` with an image that fades to transparent? Where is the "contour" of that? You would need to use some kind of contour recognition algorithm to do figure out in general. OTOH, if you only limit it to views with a `CAShapeLayer` for example, then you can get the shape layer's path. – Sweeper Dec 10 '20 at 10:11
  • @Sweeper I am subclassing UIView that's auto resizing. I am trying to add CAShapLayer to my subclassed view but it returns nil for its path. Could you please have look at my edited post? – Matthew So Dec 10 '20 at 11:00
  • 1
    The path is nil because you didn't give it a path. Anyway, are you just trying to find points on a rounded rectangle? In that case you can create the path of the contour yourself, with `UIBezierPath.init(roundedRect:cornerRadius:)`. – Sweeper Dec 10 '20 at 11:05
  • Right I added UIBezierPath's cgPath into the shapeLayer with that init and it's printing something looking promising. I will play around with it and will update what happens. – Matthew So Dec 10 '20 at 11:14
  • 1
    If you have the `CGPath`, use this solution: https://stackoverflow.com/questions/24225943/get-random-cgpoint-on-cgpath – Sweeper Dec 10 '20 at 11:16
  • Sweeper although I am still working on it I would say thank you first, for the inspiration. – Matthew So Dec 10 '20 at 11:19

0 Answers0