Core graphics - Create and draw a CALayer

0

Good, I'm having a problem that does not throw any errors by console, just Xcode goes AppDelegate with a red line. I'm trying to create a UIButton with a sublayer which should be a circle bordering the button (which would also be a circle). What I did now code house is the following:

//
//  PhotoButtonView.swift
//  PhotoChat
//
//  Created by ... on 10/9/17.
//  Copyright © 2017 ... . All rights reserved.
//

import UIKit

class PhotoButtonView: UIButton {



    override init(frame: CGRect) {
        super.init(frame: frame)

        self.clipsToBounds = false
        self.createCircleLayer(To: self)

        self.setNeedsDisplay()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.clipsToBounds = false
        self.createCircleLayer(To: self)
    }

    private func createCircleLayer(To view: UIView) {
        let layer = CALayer()
        layer.name = "CircleLayer"

        let circleLayerDelegate = CircleLayerDelegate()

        layer.delegate = circleLayerDelegate

        layer.frame.origin = CGPoint(x: 0, y: 0)//Se toma el frame del layer, en relación a las cordenadas del padre.
        layer.frame.size = view.frame.size



        self.layer.addSublayer(layer)
    }



    class CircleLayerDelegate: NSObject, CALayerDelegate {

        var fillColor: CGColor = UIColor.orange.cgColor

        func draw(_ layer: CALayer, in ctx: CGContext) {
            if layer.name == "CircleLayer" {
                ctx.setLineWidth(10)
                ctx.setFillColor(fillColor)
                ctx.setStrokeColor(fillColor)
                ctx.addEllipse(in: CGRect(x: 0, y: 0, width: 100, height: 100))
                ctx.fillPath()
                ctx.strokePath()
            }
        }  

    }

}

I know there are many ways to do it but I want to see to understand how the delegate works (which is the line that if I comment there is no error).

    
asked by MatiEzelQ 10.09.2017 в 17:38
source

1 answer

0

You can set an Exception Breakpoint so that Xcode will stop execution just before the exception, so you can better investigate what happens.

Debug / Breakpoints / Create Exception Breakpoint

    
answered by 27.09.2017 в 13:52