3

I have UIView in my first view controller. I have set that UIView class to Custom Progress View using following code.

class ProgressView: UIView {
    let trackLayer = CAShapeLayer()
    let shapeLayer = CAShapeLayer()

    static let instance = ProgressView()

    override func awakeFromNib() {
        super.awakeFromNib()
        let center = CGPoint.init(x: frame.width / 2, y: frame.height / 2)
        let circularPath = UIBezierPath(arcCenter: center, radius: 30, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)

        trackLayer.path = circularPath.cgPath
        trackLayer.strokeColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
        trackLayer.lineWidth = 3
        trackLayer.fillColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
        trackLayer.lineCap = kCALineCapRound
        self.layer.addSublayer(trackLayer)
        shapeLayer.path = circularPath.cgPath
        shapeLayer.strokeColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
        shapeLayer.lineWidth = 3
        shapeLayer.fillColor = #colorLiteral(red: 0.3794638515, green: 0.5145698786, blue: 0.9605538249, alpha: 1)
        shapeLayer.lineCap = kCALineCapRound
        shapeLayer.strokeEnd = 0
        self.layer.addSublayer(shapeLayer)
    }
}

There is also a function inside that custom UIView class which is for starting animation for filling circle with following code

 func startProgress() {
    let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
    basicAnimation.toValue = 1
    basicAnimation.duration = 60        
    basicAnimation.fillMode = kCAFillModeForwards
    basicAnimation.isRemovedOnCompletion = false
    shapeLayer.add(basicAnimation, forKey: "soBasic")
}

I need to call that startProgress function inside my First View controller. In viewDidLoad I have called ProgressView.instance.startProgress() but it is not firing. How can I call start progress function from another class?

PPL
  • 6,357
  • 1
  • 11
  • 30
atalayasa
  • 3,310
  • 25
  • 42

1 Answers1

4

In ProgressView you need to create an initialise or you can use default one.

class ProgressView: UIView {

    //you can set any additional properties in it.

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

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 } 

How to use

In view controller where you wanna use...

let pgView = ProgressView() //or any other initialise if you have created
.
.
.
pgView.startProgress()
Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • Its nice +1 from me, @Atalay Do not create UIView singletons – PPL Jun 06 '18 at 06:44
  • Thanks for comment but it gives me an error 'super.init' cannot be called outside of an initializer and Keyword 'init' cannot be used as an identifier here how can I fix it? @MahendraGP – atalayasa Jun 06 '18 at 06:58