2017-06-22 54 views
0

我有UIPageViewController 5頁。它在每個頁面上登上動畫UIViewControllers動畫。我研究了幾天,並找不到停止動畫並在滾動(換頁)後重置頁面的方法。停止鏈動畫並重置UIPageController頁面更改

我UIPageViewController設置是這樣的:

class BoardingPageViewController: UIPageViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    dataSource = self 
    delegate = self 
    setViewControllers([getStepOne()], direction: .forward, animated: false, completion: nil) 
} 

override var preferredStatusBarStyle: UIStatusBarStyle { 
    return .lightContent 
} 

func getStepOne() -> BoardView1ViewController { 
    return storyboard!.instantiateViewController(withIdentifier: "BoardView1") as! BoardView1ViewController 
} 

func getStepTwo() -> BoardView2ViewController { 
    return storyboard!.instantiateViewController(withIdentifier: "BoardView2") as! BoardView2ViewController 
} 

func getStepThree() -> BoardView3ViewController { 
    return storyboard!.instantiateViewController(withIdentifier: "BoardView3") as! BoardView3ViewController 
} 
func getStepFour() -> BoardView4ViewController { 
    return storyboard!.instantiateViewController(withIdentifier: "BoardView4") as! BoardView4ViewController 
} 
func getStepFive() -> BoardView5ViewController { 
    return storyboard!.instantiateViewController(withIdentifier: "BoardView5") as! BoardView5ViewController 
} 
} 

extension BoardingPageViewController : UIPageViewControllerDataSource { 

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { 
    if viewController.isKind(of: BoardView5ViewController.self) { 
     // 5 -> 4 
     return getStepFour() 
    } else if viewController.isKind(of: BoardView4ViewController.self) { 
     // 4 -> 3 
     return getStepThree() 
    } else if viewController.isKind(of: BoardView3ViewController.self) { 
     // 3 -> 2 
     return getStepTwo() 
    } else if viewController.isKind(of: BoardView2ViewController.self) { 
     // 2 -> 1 
     return getStepOne() 
    } else { 
     // 0 -> end of the road 
     return nil 
    } 
} 

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { 
    if viewController.isKind(of: BoardView1ViewController.self) { 
     // 0 -> 1 
     return getStepTwo() 
    } else if viewController.isKind(of: BoardView2ViewController.self) { 
     // 1 -> 2 
     return getStepThree() 
    } else if viewController.isKind(of: BoardView3ViewController.self) { 
     // 1 -> 2 
     return getStepFour() 
    } else if viewController.isKind(of: BoardView4ViewController.self) { 
     // 1 -> 2 
     return getStepFive() 
    } else { 
     // 2 -> end of the road 
     return nil 
    } 
} 

func presentationCount(for pageViewController: UIPageViewController) -> Int { 
    return 5 
} 

func presentationIndex(for pageViewController: UIPageViewController) -> Int { 
    return 0 
} 

} 

extension BoardingPageViewController : UIPageViewControllerDelegate { 

} 

而且我UIViewControllers都是用簡單的動畫類似。我嘗試通過將chainAni值切換爲false並在ViewDidDisappear上調用它來實現此目的。但是,如果頁面切換髮生在動畫中,則不起作用。

override func viewDidAppear(_ animated: Bool) { 
    chainAni = true 
    ani0() 
} 

func ani0() { 
    if chainAni != true { 
     return 
    } 
    UIView.animate(withDuration: 1, delay: 1, animations: { 
     self.handPic.alpha = 1 
    }) { (false) in 
     self.ani1() 
    } 
} 
func ani1() { 
    if chainAni != true { 
     return 
    } 
    UIView.animate(withDuration: 1.5, delay: 1, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: { 
     self.highlightAction(any: self.handPic) 
     self.highlightAction(any: self.folderBtn) 
    }) { (false) in 
     self.ani2() 
    } 
} 
func ani2() { 
    if chainAni != true { 
     return 
    } 
    UIView.animate(withDuration: 1.5, delay: 1, animations: { 
     self.unhighlightAction(any: self.handPic) 
     self.unhighlightAction(any: self.folderBtn) 
    }) { (false) in 
     self.ani3() 
    } 
} 
func ani3() { 
    if chainAni != true { 
     return 
    } 
    UIView.animate(withDuration: 0.5, delay: 0, animations: { 
     self.handPic.alpha = 0 
     self.alertImg.alpha = 1 
     self.alertImg.transform = .identity 
    }) { (false) in 
     self.ani4() 
    } 
} 

func clearAni() { 
    alertImg.image = #imageLiteral(resourceName: "alert1") 

    darkView.removeFromSuperview() 
    screenView.removeFromSuperview() 

    mainImg.alpha = 1 
    alertImg.alpha = 0 
    alert2Img.alpha = 0 
    handPic.alpha = 0 
    handPic.transform = .identity 
    hand2Pic.alpha = 0 
    hand2Pic.transform = .identity 
    newFolderImg.alpha = 0 
    newFolderImg.transform = .identity 
} 

override func viewDidDisappear(_ animated: Bool) { 
    clearAni() 
    chainAni = false 
} 
+0

有人嗎?這是不可行的嗎? –

回答

0

所以,我想我必須刪除所有的動畫從它的上海華層。

func clearAni() { 
alertImg.image = #imageLiteral(resourceName: "alert1") 

mainImg.layer.removeAllAnimations() 
mainLbl.layer.removeAllAnimations() 
alertImg.layer.removeAllAnimations() 
alert2Img.layer.removeAllAnimations() 
folderBtn.layer.removeAllAnimations() 
handPic.layer.removeAllAnimations() 
hand2Pic.layer.removeAllAnimations()   
darkView.layer.removeAllAnimations() 


mainImg.alpha = 1 
alertImg.alpha = 0 
alert2Img.alpha = 0 
handPic.alpha = 0 
handPic.transform = .identity 
hand2Pic.alpha = 0 
hand2Pic.transform = .identity 
newFolderImg.alpha = 0 
newFolderImg.transform = .identity 
}