我正在尝试在嵌套UIViewController
中的UIPageViewController
与同一UIPageViewController
的新实例之间创建转换。
为了使它更清晰,我试图实现的形象:
我目前拥有的是UIViewController
,其UIPageViewController
视图为子视图。
UIPageViewController
由UIViewController
个UIImageView
组成。{/ 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。 imageFrame
为an extension on UIImageView
,返回显示的UIImage
的实际框架。
虽然这确实有效,但我觉得这不是最佳做法。
所以我在这里可以做得更好的任何指示都会受到赞赏。
类似的事情;