1

I made a function to make a circular animation. Function takes radius:float as input. I have done this in a ViewController Class and it is working fine for that view controller. But now i want to use this animation on multiple view controllers and don't want to write the same code on every single view controller. So i want to know how can i make this function in a separate file in such a way that i only need to call the function with the radius and it will do the work. Or can you tell me the best practice to do that. Thanks in advance.

//

I dont want to do it in myViewController i just want to create a new class only for circular animation. and also dont want to import that class want to do like this-

import UIKit
class CircularProgressView {
    private let shapeLayer = CAShapeLayer()
    public func createProgressView(radius:CGFloat,forView:UIView) {
        let center = forView.center
        let circularPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: -CGFloat.pi/2, endAngle: 2*CGFloat.pi, clockwise: true)

        let trackLayer = CAShapeLayer()
        trackLayer.path = circularPath.cgPath
        trackLayer.strokeColor = UIColor.lightGray.cgColor
        trackLayer.fillColor = UIColor.clear.cgColor
        trackLayer.lineWidth = 10
        forView.layer.addSublayer(trackLayer)

        shapeLayer.path = circularPath.cgPath
        shapeLayer.strokeColor = UIColor.red.cgColor
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.lineWidth = 10
        shapeLayer.lineCap = .round
        shapeLayer.strokeEnd = 0
        forView.layer.addSublayer(shapeLayer)
        forView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
    }
    @objc private func handleTap() {
        print("hello s")
        let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
        basicAnimation.toValue = 1
        basicAnimation.duration = 2

        basicAnimation.fillMode = .forwards
        basicAnimation.isRemovedOnCompletion = false

        shapeLayer.add(basicAnimation, forKey: "basic")
    }
}

and use this like- import UIKit

class ViewController1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        CircularProgressView().createProgressView(radius: 50, forView: view)
    }

}

but in this code guesture recogniser is not working.

3
  • inherited all your view controllers from viewController where you implemented your animation Commented Jan 16, 2020 at 11:27
  • can you provide an example. Commented Jan 16, 2020 at 11:28
  • @PiyushSharma please see the answers below Commented Jan 16, 2020 at 11:46

4 Answers 4

1

You can simply create a UIViewController extension and add a method animate(with:) in it with the relevant code,

extension UIViewController {
    func animate(with radius: Float) {
        //add your code here..
    }
}

The method animate(with:) is now available to all UIViewController subclasses. So, you can simply call it like so

class VC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.animate(with: 10.0)
    }
}

There is no need to create any parent class in this case.

Sign up to request clarification or add additional context in comments.

Comments

1

Create a new swift file called BaseViewController

import UIKit
class BaseViewController : UIViewController {
    //Add your animation code
}

And inherit from this new file

import UIKit 
class YourVC : BaseViewController {
  // do your stuff here
}

Another way to do it is by creating an extension.

extension UIViewController {
   func doAnimation() {
      // do your stuff here
   }
}

And to use it.

view.doAnimation()

You can also have look at Swift Documention on Inheritance

2 Comments

You don't need to subclass a view controller to reuse a view.
@Desdenova updated my answer with another approach.
0

Here is some example of how you can do it. Hope it helps

    class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func startCircularAnimation(radius: Float) {
        // your animation code here
    }

}

class FirstViewController: MyViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        startCircularAnimation(radius: 25)
    }

}

class SecondViewController: MyViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        startCircularAnimation(radius: 30)
    }

}

Comments

0

I assume you try to call function once for to use whole view controllers. In order to achieve that you can create Manager class. with Manager.Instance.callFunction you can use your animations inside every controller class.

class Manager {
    private static let _instance =  Manager()



     static var  Instance: Manager{

        return _instance
    }

    private init(){

    }
  func callFunction(){

 // add animation stuff.

    }

2 Comments

@PiyushSharma I saw your edited question. My answer is correct. Inside your viewController viewDidLoad call this Manager.Instance.callFunction.
put all your CircleProgress process inside to callFunction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.