user
user

Reputation: 83

swift: call action after implementing another action

I have two actions.

I want to first action will be implemented and after that second action will be call. If I add action like this:

func actions {
    action1()
    action2()
}

but in my code and action implemented at same time.

I have this code in first action:

let pageController = self.storyboard?.instantiateViewController(withIdentifier: "PageViewController") as! UIPageViewController

pageController.dataSource = self
pageController.delegate = self

firstPage = 0
secondPage = 1

guard let firstController = getContentViewController(withIndex: firstPage) else { return }
guard let secondController = getContentViewController(withIndex: secondPage) else { return }

pageViewController?.setViewControllers([firstController, secondController], direction: .reverse, animated: true, completion: nil)

It is a flip animation.

If I use this

func actions {
    action1()
    action2()
}

My flip animation will not be able to show.

Maybe I need to use timer?

Upvotes: 1

Views: 772

Answers (1)

Sharkes Monken
Sharkes Monken

Reputation: 670

You can include callbacks on each function in such once functional operations are completed they invoke the other functions. eg:

Create a callback function

func action1(complete: @escaping (_ status: Bool) {
    // animation operation 

    // Notify callback 
    complete(true)
}

How to use callback functions

action1(complete: { (animationCompleted)
   if animationCompleted {
     // Invoke second animation function 
     // Remember use `self` instance once your inside a callback method
     self.action2() 
   }
})

Upvotes: 1

Related Questions