Sunday, September 10, 2023
HomeiOS Developmentios - Add picture insde CAShapeLayer

ios – Add picture insde CAShapeLayer


So I’m at the moment attempting so as to add a picture contained in the circularShapeLayer within the under code.

Right here is the complete code:

import Basis
import UIKit

@IBDesignable
class PlainHorizontalProgressBar: UIView {
    @IBInspectable var shade: UIColor = .grey {
        didSet { setNeedsDisplay() }
    }

    var progress: CGFloat = 0 {
        didSet { setNeedsDisplay() }
    }

    var circularShapeColor: UIColor = .blue {
        didSet { setNeedsDisplay() }
    }

    non-public let progressLayer = CALayer()
    non-public let backgroundMask = CAShapeLayer()
    non-public let circularShapeLayer = CAShapeLayer()
    
    // Create an UIImageView for the system picture
    non-public let systemImageView = UIImageView()

    override init(body: CGRect) {
        tremendous.init(body: body)
        circularShapeColor = UIColor.systemOrange
        setupLayers()
    }

    required init?(coder: NSCoder) {
        tremendous.init(coder: coder)
        circularShapeColor = UIColor.systemOrange
        setupLayers()
    }

    non-public func setupLayers() {
        layer.addSublayer(progressLayer)
        layer.addSublayer(circularShapeLayer)
        
        // Add the system picture view to the circularShapeLayer
        circularShapeLayer.addSublayer(systemImageView.layer)
    }

    override func draw(_ rect: CGRect) {
        backgroundMask.path = UIBezierPath(roundedRect: rect, cornerRadius: rect.top * 0.75).cgPath
        layer.masks = backgroundMask
        
        let progressRect = CGRect(origin: .zero, dimension: CGSize(width: rect.width * progress, top: rect.top))
        
        progressLayer.body = progressRect
        progressLayer.backgroundColor = shade.cgColor
        
        // Calculate the place and dimension of the round form
        let circularSize = CGSize(width: rect.top, top: rect.top)
        let circularOrigin = CGPoint(x: progressRect.maxX - circularSize.width / 2.0, y: (rect.top - circularSize.top) / 2.0)
        let circularPath = UIBezierPath(ovalIn: CGRect(origin: circularOrigin, dimension: circularSize))
        circularShapeLayer.path = circularPath.cgPath
        
        // Set the body and picture for the system picture view
        systemImageView.body = circularShapeLayer.bounds
        systemImageView.picture = UIImage(systemName: "bolt.fill") // Set the system picture
        systemImageView.tintColor = .white // Set the picture shade
    }
}

However one way or the other the picture is just not displaying. I’ve tried altering the tintColor of the picture, and tried with different pictures additionally. What am I doing incorrect? Any options or concepts on how can I accomplish this?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments