2017-06-22 63 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 
}