【发布时间】:2018-05-25 13:56:15
【问题描述】:
刚刚了解了如何创建完成处理程序,我在原则上理解它们,但我不知道如何将这些想法付诸实践以完成我所需要的。
我有以下通用代码和故事板结构:sessionVC(一个 UIViewController)及其 UIView 拥有一个容器视图,其中包含一个嵌入到 animationVC(也是 UIViewController)及其 SKView 的 segue。
从 sessionVC 我想在 animationVC 的 SKView 中运行一系列动画。我希望尽快准备好每个动画(例如,在每个之前的动画仍在运行时),并且我希望每个动画在开始之前等待最后一个动画完成。请参阅下面的代码。
我的问题是,我应该在我的代码中放置什么来代替 ???s 来完成我想要的效果(上面提到,以及在代码 cmets 中的每个 *** 之后)?
// TO DELEGATE ANIMATION TO animationVC
protocol AnimateContentDelegate: AnyObject {
prepareContent(_ Content, contentWasPrepared: ???)
animateContent(animationDidComplete: ???)
playAnimation()
pauseAnimation()
}
// CONTROL THE OVERALL SESSION
class sessionVC: UIViewController {
// INITIALIZE contentArray
weak var delegate: AnimateContentDelegate?
override func viewDidLoad {
super.viewDidLoad()
delegate = self.childViewControllers[0] as? AnimateContentDelegate
runSession(contentArray)
}
func runSession(_ contentArray) {
for content in contentArray {
delegate?.prepareContent(content, contentWasPrepared: ???)
// ***DON’T START THE NEXT ANIMATION UNTIL contentWasPrepared
// DO CONTINUE THE CURRENT ANIMATION, AND ALLOW INTERACTIONS
delegate?.animateContent(animationDidComplete: ???)
// ***DON’T START THE NEXT ANIMATION UNTIL animationDidComplete
// DO CONTINUE THE CURRENT ANIMATION, AND ALLOW INTERACTIONS
}
}
@IBAction func playOrPause(_ sender: UILongPressGestureRecognizer) {
if sender == .possible || sender.state == .ended {
delegate?.playAnimation()
} else if sender.state == .began {
delegate?.pauseAnimation()
}
}
}
// PREPARE AND ANIMATE CURRENT CONTENT
class animationVC: UIViewController, AnimateContentDelegate {
// SET UP SKVIEW
func prepareContent(_ content: Content, prepCompleteHandler: ???) {
// PREPARE THE CONTENT
// ***REPORT WHEN IT IS FINISHED
}
func animateContent(animationCompleteHandler: ???) {
// ANIMATE THE CONTENT
// ***REPORT IF IT RAN TO COMPLETION
}
func playAnimation() {
skView?.scene?.isPaused = false
}
func pauseAnimation() {
skView?.scene?.isPaused = true
}
}
【问题讨论】:
标签: ios swift completionhandler