如何从嵌套的UIPageViewController转换为呈现的UIPageViewController

时间:2015-09-03 14:53:03

标签: ios swift uiviewcontroller uiimageview uipageviewcontroller

我正在尝试在嵌套UIViewController中的UIPageViewController与同一UIPageViewController的新实例之间创建转换。

为了使它更清晰,我试图实现的形象:

mockup

我目前拥有的是UIViewController,其UIPageViewController视图为子视图。

UIPageViewControllerUIViewControllerUIImageView组成。{/ 1}}。

现在,当我点按UIPageViewController中的某个页面时,它会显示同一个UIPageViewController的新实例。

我的实施:

我自定义了从呈现到呈现的视图控制器的转换。

然后我发现UIViewController中没有来自 UIViewControllerAnimatedTransitioning,所以我将其手动传递给我的动画师类。

我的源代码管理器:

class ProfileViewController: UIViewController, UIViewControllerTransitioningDelegate {

    var animator = Animator()

    var photosViewController: ProfilePhotosViewController!

    var model: Person!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.darkGrayColor()

        self.photosViewController = ProfilePhotosViewController()
        self.photosViewController.willMoveToParentViewController(self)
        self.addChildViewController(self.photosViewController)
        self.view.addSubview(self.photosViewController.view)
        self.photosViewController.didMoveToParentViewController(self)

        self.photosViewController.view.setTranslatesAutoresizingMaskIntoConstraints(false)

        let views = ["imageView": self.photosViewController.view]
        self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[imageView]|", options: nil, metrics: nil, views: views))
        self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[imageView(==200)]", options: nil, metrics: nil, views: views))

        self.photosViewController.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector("tapHandler:")))
    }

    func tapHandler(recognizer: UITapGestureRecognizer) {
        let photosViewController = ProfilePhotosViewController()

        photosViewController.photos = self.model.photos
        photosViewController.index = self.photosViewController.index

        photosViewController.modalPresentationStyle = .Custom
        photosViewController.transitioningDelegate = self
        self.photosViewController.viewControllers.first!.presentViewController(photosViewController, animated: true, completion: nil)
    }

    func animationControllerForPresentedController(presented: UIViewController,
                             presentingController presenting: UIViewController,
                                     sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.animator.presentingController = source
        return self.animator
    }

}

我的动画师课程:

class Animator: NSObject, UIViewControllerAnimatedTransitioning {

    var presentingController: UIViewController?

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return 0.25
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        var toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
        var toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

        var fromViewController = self.presentingController!
        var fromView = fromViewController.view

        transitionContext.containerView().addSubview(toView)

        let snapshotFrame = (fromView as! UIImageView).imageFrame
        let snapshot: UIView? = fromView?.resizableSnapshotViewFromRect(snapshotFrame, afterScreenUpdates: false, withCapInsets: UIEdgeInsetsZero)
        snapshot!.frame = fromView!.convertRect(snapshotFrame, toView: toView)
        transitionContext.containerView().addSubview(snapshot!)

        fromView.hidden = true

        toView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0)

        (toView.subviews[0] as! UIView).hidden = true

        let fromImageViewController = (toViewController as? ProfilePhotosViewController)!.viewControllers.first
        let fromImageView = fromImageViewController?.view as! UIImageView

        toView.layoutIfNeeded()

        var toImageFrame = fromImageView.imageFrame

        UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: {
            toView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(1)

            snapshot?.frame = toImageFrame

            }, completion: { finished in
                (toView.subviews[0] as! UIView).hidden = false
                fromView?.hidden = false
                snapshot?.removeFromSuperview()
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        })
    }

}

N.B。 imageFramean extension on UIImageView,返回显示的UIImage的实际框架。

虽然这确实有效,但我觉得这不是最佳做法。

所以我在这里可以做得更好的任何指示都会受到赞赏。

类似的事情;

  • 是一个模态呈现的视图控制器正确的解决方案,还是应该使用自定义的推/弹转换?
  • 我真的需要创建一个这个视图控制器的新实例,还是有办法让它“弹出”当前视图?

0 个答案:

没有答案