Search code examples
iosswiftuiviewuipagecontrol

subView of UIPageViewController not animating Subview? Swift 4


after many frustrating hours and an unhappy client, I have decided to come here.

Here is my viewDidAppear from the my pageController's first subview class

 override func viewDidAppear(_ animated: Bool) {
       print("hello")
     self.view.layoutIfNeeded()
  self.averageWorkSessions.shapeLayer.strokeEnd = 0
    UIView.animate(withDuration: 0.5) {
        let average = CGFloat(self.averageWorkSessions.average / 10)
        self.averageWorkSessions.shapeLayer.strokeEnd = average
        if !(average > 0.0){
            self.averageWorkSessions.Title.text = "N/A"
        }
        self.view.layoutIfNeeded()
    }
}

Consider this

The averageWorkSessions is a custom view with custom components , but it is still a subclass of UIView

Here is part of page controller that could relate to this problem in anyway

  lazy var subViewControllers: [UIViewController] = {

    return [

    UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Stats1") as! StatisticsViewController ,
     UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Stats2") as! Statistics2ViewController ,
       UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Stats3") as! Statistics3ViewController
    ]

}()

override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
    self.dataSource = self
    setViewControllers([subViewControllers[0]], direction: .forward , animated: true, completion: nil)
    self.view.layoutIfNeeded()
}

Short version

Why is this not animating properly? the print "hello" works exactly as expected... why not my animation?


Solution

  • I believe the issue is that you are setting the self.averageWorkSessions.shapeLayer.strokeEnd inside the UIView.animate()

    The self.view.layoutIfNeeded() should be inside the UIView.animate(), but setting the constants should not be.