UIView.animate没有沿正确的方向滑动

时间:2018-12-28 19:47:44

标签: swift uiviewcontroller uiviewanimation

我有一个tabBarController连接到3个ViewController,每个ViewController都嵌入在NavigationController中。我进行了设置,以使自定义ViewController之一在单击tabBar时将在当前ViewController的前面向上滑动。一切正常。

但是,自定义ViewController具有一个“取消”按钮,该按钮应使其向下滑动并显示旧的ViewController。无论我尝试采用哪种方式,动画都不会朝我需要的方向滑动。由于某种原因,ViewController在屏幕外启动,然后尝试在屏幕上滑动。滑上后,它会消失并显示旧的视图控制器。

我希望它在屏幕上启动并向下滑动。

TabBarController中的动画代码:

 func animateToTab(toIndex: Int) -> Bool {
        guard let tabViewControllers = viewControllers,
            let selectedVC = selectedViewController else { return false}

        guard let fromView = selectedVC.view,
            let toView = tabViewControllers[toIndex].view,
            let fromIndex = tabViewControllers.index(of: selectedVC),
            fromIndex != toIndex else { return false}


        // Add the toView to the tab bar view
        fromView.superview?.addSubview(toView)

        // Position toView off screen (above subview)
        let screenHeight = UIScreen.main.bounds.size.height
        let offset = -screenHeight
        toView.center = CGPoint(x: fromView.center.x, y: toView.center.y - offset)

        // Disable interaction during animation
        view.isUserInteractionEnabled = false

        UIView.animate(withDuration: 0.3,
                       delay: 0.1,
                       options: .curveEaseOut,
                       animations: {
                        // Slide the views by -offset
                        toView.center = CGPoint(x: toView.center.x, y: toView.center.y  + offset)

        }, completion: { finished in
            // Remove the old view from the tabbar view.
            fromView.removeFromSuperview()
            self.selectedIndex = toIndex
            self.view.isUserInteractionEnabled = true
        })
        return true
    }

ViewController中的动画代码,由IBAction函数调用。

func animateToTab(toIndex: Int){
        let tabBar = self.tabBarController!
        guard let tabViewControllers = tabBar.viewControllers,
            let selectedVC = tabBar.selectedViewController else { return }

        guard let fromView = selectedVC.view,
            let toView = tabViewControllers[toIndex].view,
            let fromIndex = tabViewControllers.index(of: selectedVC),
            fromIndex != toIndex else { return }


        // Add the toView to the tab bar view
        toView.superview?.addSubview(fromView)

        // Position fromView on screen
        let screenHeight = UIScreen.main.bounds.size.height
        let offset = -screenHeight
        fromView.center = CGPoint(x: toView.center.x, y: fromView.center.y)
        // Disable interaction during animation
        tabBar.view.isUserInteractionEnabled = false

        UIView.animate(withDuration: 0.3,
                       delay: 0.1,
                       options: .curveEaseOut,
                       animations: {
                        // Slide the views by -offset
                        fromView.center = CGPoint(x: toView.center.x, y: fromView.center.y + offset)

        }, completion: { finished in
//             Remove the old view from the tabbar view.
            fromView.removeFromSuperview()
            tabBar.selectedIndex = toIndex
            tabBar.view.isUserInteractionEnabled = true
        })
    }

0 个答案:

没有答案