public override void AnimateTransition(IUIViewControllerContextTransitioning transitionContext) { var fromViewController = transitionContext.GetViewControllerForKey(UITransitionContext.FromViewControllerKey); var toViewController = transitionContext.GetViewControllerForKey(UITransitionContext.ToViewControllerKey); UIView containerView = transitionContext.ContainerView; var fromView = transitionContext.GetViewFor(UITransitionContext.FromViewKey); var toView = transitionContext.GetViewFor(UITransitionContext.ToViewKey); fromView.Frame = transitionContext.GetInitialFrameForViewController(fromViewController); toView.Frame = transitionContext.GetFinalFrameForViewController(toViewController); fromView.Alpha = 1f; toView.Alpha = 0f; containerView.AddSubview(toView); var transitionDuration = TransitionDuration(transitionContext); UIView.Animate(transitionDuration, 0, UIViewAnimationOptions.TransitionNone, () => { fromView.Alpha = 0f; toView.Alpha = 1f; }, () => { bool wasCancel = transitionContext.TransitionWasCancelled; transitionContext.CompleteTransition(!wasCancel); } ); }
protected override void AnimatePresentingInContext(IUIViewControllerContextTransitioning transitioningContext, UIViewController originatingController, UIViewController destinationController) { CGRect fromVCrect = transitioningContext.GetInitialFrameForViewController(originatingController); CGRect toVCRect = fromVCrect; toVCRect.Y = (nfloat)(toVCRect.Size.Height - this.InitialY); destinationController.View.Frame = toVCRect; UIView container = transitioningContext.ContainerView; UIView imageView = new UIImageView(DummyImage); destinationController.View.AddSubview(imageView); container.AddSubview(originatingController.View); container.AddSubview(destinationController.View); UIView.Animate(AnimationDuration, () => { destinationController.View.Frame = fromVCrect; imageView.Alpha = 0.0f; }, () => { imageView.RemoveFromSuperview(); if (transitioningContext.TransitionWasCancelled) { transitioningContext.CompleteTransition(false); } else { transitioningContext.CompleteTransition(true); } }); }
public override void AnimateTransition (IUIViewControllerContextTransitioning transitionContext, UIViewController fromViewController, UIViewController toViewController, UIView fromView, UIView toView) { UIView containerView = transitionContext.ContainerView; containerView.AddSubview (toView); containerView.SendSubviewToBack (toView); var transform = CATransform3D.Identity; transform.m34 = -0.002f; containerView.Layer.SublayerTransform = transform; var initialFrame = transitionContext.GetInitialFrameForViewController (fromViewController); fromView.Frame = initialFrame; toView.Frame = initialFrame; List<UIView> toViewSnapshots = CreateSnapshots (toView, true); UIView flippedSectionOfToView = toViewSnapshots [Reverse ? 0 : 1]; List<UIView> fromViewSnapshots = CreateSnapshots (fromView, false); UIView flippedSectionOfFromView = fromViewSnapshots [Reverse ? 1 : 0]; flippedSectionOfFromView = AddShadowToView (flippedSectionOfFromView, !Reverse); UIView flippedSectionOfFromViewShadow = flippedSectionOfFromView.Subviews [1]; flippedSectionOfFromViewShadow.Alpha = 0f; flippedSectionOfToView = AddShadowToView (flippedSectionOfToView, Reverse); UIView flippedSectionOfToViewShadow = flippedSectionOfToView.Subviews [1]; flippedSectionOfToViewShadow.Alpha = 1f; // change the anchor point so that the view rotate around the correct edge UpdateAnchorPointAndOffset (new CGPoint (Reverse ? 0f : 1f, 0.5f), flippedSectionOfFromView); UpdateAnchorPointAndOffset (new CGPoint (Reverse ? 1f : 0f, 0.5f), flippedSectionOfToView); flippedSectionOfToView.Layer.Transform = Rotate (Reverse ? (float)Math.PI / 2 : -(float)Math.PI / 2); double duration = TransitionDuration (transitionContext); Action animations = () => { UIView.AddKeyframeWithRelativeStartTime (0.0, 0.5, () => { flippedSectionOfFromView.Layer.Transform = Rotate (Reverse ? -(float)Math.PI / 2 : (float)Math.PI / 2); flippedSectionOfFromViewShadow.Alpha = 1f; }); UIView.AddKeyframeWithRelativeStartTime (0.5, 0.5, () => { flippedSectionOfToView.Layer.Transform = Rotate (Reverse ? 0.001f : -0.001f); flippedSectionOfToViewShadow.Alpha = 0f; }); }; UIView.AnimateKeyframes (duration, 0.0, UIViewKeyframeAnimationOptions.CalculationModeLinear, animations, (finished) => { if (transitionContext.TransitionWasCancelled) { RemoveOtherViews (fromView); } else { RemoveOtherViews (toView); } transitionContext.CompleteTransition (!transitionContext.TransitionWasCancelled); }); }
public void AnimateTransition(IUIViewControllerContextTransitioning transitionContext) { var fromViewController = transitionContext.GetViewControllerForKey(UITransitionContext.FromViewControllerKey); var toViewController = transitionContext.GetViewControllerForKey(UITransitionContext.ToViewControllerKey); UIView containerView = transitionContext.ContainerView; var toView = transitionContext.GetViewFor(UITransitionContext.ToViewKey); var fromView = transitionContext.GetViewFor(UITransitionContext.FromViewKey); bool isPresenting = (fromViewController == PresentingViewController); var fromViewFinalFrame = transitionContext.GetFinalFrameForViewController(fromViewController); var toViewInitialFrame = transitionContext.GetInitialFrameForViewController(toViewController); var toViewFinalFrame = transitionContext.GetFinalFrameForViewController(toViewController); if (toView != null) { containerView.AddSubview(toView); } if (isPresenting) { toViewInitialFrame.X = containerView.Bounds.GetMinX(); toViewInitialFrame.Y = containerView.Bounds.GetMaxY(); toViewInitialFrame.Size = toViewFinalFrame.Size; toView.Frame = toViewInitialFrame; } else { fromView.Frame.Offset(0f, fromView.Frame.Height); fromViewFinalFrame = new CGRect(fromView.Frame.X, fromView.Frame.Y + fromView.Frame.Height, fromViewFinalFrame.Width, fromViewFinalFrame.Height); } double transitionDuration = TransitionDuration(transitionContext); UIView.Animate(transitionDuration, 0, UIViewAnimationOptions.TransitionNone, () => { if (isPresenting) { toView.Frame = toViewFinalFrame; } else { fromView.Frame = fromViewFinalFrame; } }, () => { bool wasCancelled = transitionContext.TransitionWasCancelled; transitionContext.CompleteTransition(!wasCancelled); } ); }
public void AnimateTransition(IUIViewControllerContextTransitioning transitionContext) { var toController = transitionContext.GetViewControllerForKey(UITransitionContext.ToViewControllerKey); var fromController = transitionContext.GetViewControllerForKey(UITransitionContext.FromViewControllerKey); var animationDuration = TransitionDuration(transitionContext); if (presenting) { transitionContext.ContainerView.AddSubview(toController.View); var finalFrame = transitionContext.GetFinalFrameForViewController(toController); var frame = new CGRect(finalFrame.Location, finalFrame.Size); frame.Offset(0.0f, transitionContext.ContainerView.Frame.Height - 20); toController.View.Frame = frame; toController.View.Alpha = 0.5f; AnimationExtensions.Animate(animationDuration, Curves.CardInCurve, () => { toController.View.Frame = finalFrame; toController.View.Alpha = 1.0f; }, () => transitionContext.CompleteTransition(!transitionContext.TransitionWasCancelled)); } else { var initialFrame = transitionContext.GetInitialFrameForViewController(fromController); initialFrame.Offset(0.0f, transitionContext.ContainerView.Frame.Height); var finalFrame = initialFrame; if (transitionContext.IsInteractive) { UIView.Animate( animationDuration, () => fromController.View.Frame = finalFrame, () => transitionContext.CompleteTransition(!transitionContext.TransitionWasCancelled) ); } else { AnimationExtensions.Animate(animationDuration, Curves.CardOutCurve, () => { fromController.View.Frame = finalFrame; fromController.View.Alpha = 0.5f; }, () => transitionContext.CompleteTransition(!transitionContext.TransitionWasCancelled)); } } }
public override void AnimateTransition(IUIViewControllerContextTransitioning transitionContext) { var toViewController = GetViewController(transitionContext, UITransitionContext.ToViewControllerKey); var fromViewController = GetViewController(transitionContext, UITransitionContext.FromViewControllerKey); UIView fromView = fromViewController.View; UIView toView = toViewController.View; UIView containerView = transitionContext.ContainerView; containerView.AddSubview(toView); // Add a perspective transform var transform = CATransform3D.Identity; transform.m34 = -0.002f; containerView.Layer.SublayerTransform = transform; // Give both VCs the same start frame CGRect initialFrame = transitionContext.GetInitialFrameForViewController(fromViewController); fromView.Frame = initialFrame; toView.Frame = initialFrame; float factor = Reverse ? 1f : -1f; // flip the to VC halfway round - hiding it toView.Layer.Transform = GetRotation(factor * -(float)Math.PI / 2); double duration = TransitionDuration(transitionContext); Action animations = () => { UIView.AddKeyframeWithRelativeStartTime(0.0, 0.5, () => { fromView.Layer.Transform = GetRotation(factor * (float)Math.PI / 2); }); UIView.AddKeyframeWithRelativeStartTime(0.5, 0.5, () => { toView.Layer.Transform = GetRotation(0f); }); }; UIView.AnimateKeyframes(duration, 0.0, UIViewKeyframeAnimationOptions.CalculationModeLinear, animations, (finished) => { transitionContext.CompleteTransition(!transitionContext.TransitionWasCancelled); }); }
public override void AnimateTransition(IUIViewControllerContextTransitioning transitionContext) { var fromViewController = transitionContext.GetViewControllerForKey(UITransitionContext.FromViewControllerKey); var toViewController = transitionContext.GetViewControllerForKey(UITransitionContext.ToViewControllerKey); UIView containerView = transitionContext.ContainerView; var fromView = transitionContext.GetViewFor(UITransitionContext.FromViewKey); var toView = transitionContext.GetViewFor(UITransitionContext.ToViewKey); var fromFrame = transitionContext.GetInitialFrameForViewController(fromViewController); var toFrame = transitionContext.GetFinalFrameForViewController(toViewController); var offset = new CGVector(0f, 0f); if (targetEdge == UIRectEdge.Left) { offset = new CGVector(-1f, 0f); } else if (targetEdge == UIRectEdge.Right) { offset = new CGVector(1f, 0f); } fromView.Frame = fromFrame; CGRect auxFrame = toFrame; auxFrame.Offset(toFrame.Width * offset.dx * -1f, toFrame.Height * offset.dy * -1f); toView.Frame = auxFrame; containerView.AddSubview(toView); var duration = TransitionDuration(transitionContext); UIView.Animate(duration, 0, UIViewAnimationOptions.TransitionNone, () => { var fromFrameAux = fromFrame; fromFrameAux.Offset(fromFrame.Width * offset.dx, fromFrame.Height * offset.dy); fromView.Frame = fromFrameAux; toView.Frame = toFrame; }, () => { transitionContext.CompleteTransition(!transitionContext.TransitionWasCancelled); } ); }
public override void AnimateTransition(IUIViewControllerContextTransitioning transitionContext, UIViewController fromViewController, UIViewController toViewController, UIView fromView, UIView toView) { UIView containerView = transitionContext.ContainerView; containerView.AddSubview (toView); // Add a perspective transform var transform = CATransform3D.Identity; transform.m34 = -0.002f; containerView.Layer.SublayerTransform = transform; // Give both VCs the same start frame RectangleF initialFrame = transitionContext.GetInitialFrameForViewController (fromViewController); fromView.Frame = initialFrame; toView.Frame = initialFrame; float factor = Reverse ? 1f : -1f; // flip the to VC halfway round - hiding it toView.Layer.Transform = Rotate (factor * -(float)Math.PI / 2); double duration = TransitionDuration (transitionContext); NSAction animations = () => { UIView.AddKeyframeWithRelativeStartTime (0.0, 0.5, () => { fromView.Layer.Transform = Rotate (factor * (float)Math.PI / 2); }); UIView.AddKeyframeWithRelativeStartTime (0.5, 0.5, () => { toView.Layer.Transform = Rotate (0f); }); }; UIView.AnimateKeyframes (duration, 0.0, UIViewKeyframeAnimationOptions.CalculationModeLinear, animations, (finished) => { transitionContext.CompleteTransition (!transitionContext.TransitionWasCancelled); }); }
public override void AnimateTransition(IUIViewControllerContextTransitioning transitionContext, UIViewController fromViewController, UIViewController toViewController, UIView fromView, UIView toView) { UIView containerView = transitionContext.ContainerView; containerView.AddSubview(toView); containerView.SendSubviewToBack(toView); var transform = CATransform3D.Identity; transform.m34 = -0.002f; containerView.Layer.SublayerTransform = transform; var initialFrame = transitionContext.GetInitialFrameForViewController(fromViewController); fromView.Frame = initialFrame; toView.Frame = initialFrame; List <UIView> toViewSnapshots = CreateSnapshots(toView, true); UIView flippedSectionOfToView = toViewSnapshots [Reverse ? 0 : 1]; List <UIView> fromViewSnapshots = CreateSnapshots(fromView, false); UIView flippedSectionOfFromView = fromViewSnapshots [Reverse ? 1 : 0]; flippedSectionOfFromView = AddShadowToView(flippedSectionOfFromView, !Reverse); UIView flippedSectionOfFromViewShadow = flippedSectionOfFromView.Subviews [1]; flippedSectionOfFromViewShadow.Alpha = 0f; flippedSectionOfToView = AddShadowToView(flippedSectionOfToView, Reverse); UIView flippedSectionOfToViewShadow = flippedSectionOfToView.Subviews [1]; flippedSectionOfToViewShadow.Alpha = 1f; // change the anchor point so that the view rotate around the correct edge UpdateAnchorPointAndOffset(new CGPoint(Reverse ? 0f : 1f, 0.5f), flippedSectionOfFromView); UpdateAnchorPointAndOffset(new CGPoint(Reverse ? 1f : 0f, 0.5f), flippedSectionOfToView); flippedSectionOfToView.Layer.Transform = Rotate(Reverse ? (float)Math.PI / 2 : -(float)Math.PI / 2); double duration = TransitionDuration(transitionContext); Action animations = () => { UIView.AddKeyframeWithRelativeStartTime(0.0, 0.5, () => { flippedSectionOfFromView.Layer.Transform = Rotate(Reverse ? -(float)Math.PI / 2 : (float)Math.PI / 2); flippedSectionOfFromViewShadow.Alpha = 1f; }); UIView.AddKeyframeWithRelativeStartTime(0.5, 0.5, () => { flippedSectionOfToView.Layer.Transform = Rotate(Reverse ? 0.001f : -0.001f); flippedSectionOfToViewShadow.Alpha = 0f; }); }; UIView.AnimateKeyframes(duration, 0.0, UIViewKeyframeAnimationOptions.CalculationModeLinear, animations, (finished) => { if (transitionContext.TransitionWasCancelled) { RemoveOtherViews(fromView); } else { RemoveOtherViews(toView); } transitionContext.CompleteTransition(!transitionContext.TransitionWasCancelled); }); }
public override void AnimateTransition(IUIViewControllerContextTransitioning transitionContext) { var fromViewController = transitionContext.GetViewControllerForKey(UITransitionContext.FromViewControllerKey); var toViewController = transitionContext.GetViewControllerForKey(UITransitionContext.ToViewControllerKey); UIView containerView = transitionContext.ContainerView; var fromView = transitionContext.GetViewFor(UITransitionContext.FromViewKey); var toView = transitionContext.GetViewFor(UITransitionContext.ToViewKey); fromView.Frame = transitionContext.GetInitialFrameForViewController(fromViewController); toView.Frame = transitionContext.GetFinalFrameForViewController(toViewController); bool isPush = Array.IndexOf(toViewController.NavigationController.ViewControllers, toViewController) > Array.IndexOf(fromViewController.NavigationController.ViewControllers, fromViewController); containerView.AddSubview(toView); // TODO UIImage fromViewSnapshot; var toViewSnapshot = new UIImage(); UIGraphics.BeginImageContextWithOptions(containerView.Bounds.Size, true, containerView.Window.Screen.Scale); fromView.DrawViewHierarchy(containerView.Bounds, false); fromViewSnapshot = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); CoreFoundation.DispatchQueue.MainQueue.DispatchAsync(() => { UIGraphics.BeginImageContextWithOptions(containerView.Bounds.Size, true, containerView.Window.Screen.Scale); toView.DrawViewHierarchy(containerView.Bounds, false); toViewSnapshot = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); }); var transitionContainer = new UIView(containerView.Bounds) { Opaque = true, BackgroundColor = UIColor.Black }; containerView.AddSubview(transitionContainer); CATransform3D t = CATransform3D.Identity; t.m34 = 1f / -900f; transitionContainer.Layer.SublayerTransform = t; // The size and number of slices is a function of the width. var sliceSize = Math.Round(transitionContainer.Frame.Width / 10f); var horizontalSileces = Math.Ceiling(transitionContainer.Frame.Width / sliceSize); var verticalSlices = Math.Ceiling(transitionContainer.Frame.Height / sliceSize); // transitionSpacing controls the transition duration for each slice. // Higher values produce longer animations with multiple slices having // their animations 'in flight' simultaneously. float transitionSpacing = 160f; var transitionDuration = TransitionDuration(transitionContext); nfloat dx = isPush ? transitionContainer.Bounds.GetMaxY() - transitionContainer.Bounds.GetMinX() : transitionContainer.Bounds.GetMinX() - transitionContainer.Bounds.GetMaxX(); nfloat dy = isPush ? transitionContainer.Bounds.GetMaxY() - transitionContainer.Bounds.GetMinY() : transitionContainer.Bounds.GetMinY() - transitionContainer.Bounds.GetMaxY(); var transitionVector = new CGVector(dx, dy); var transitionVectorLength = (nfloat)Math.Sqrt(transitionVector.dx * transitionVector.dx + transitionVector.dy * transitionVector.dy); var transitionUnitVector = new CGVector(transitionVector.dx / transitionVectorLength, transitionVector.dy / new nfloat(transitionVectorLength)); for (int y = 0; y < verticalSlices; y++) { for (int x = 0; x < horizontalSileces; x++) { var fromContentLayer = new CALayer { Frame = new CGRect(x + sliceSize * -1f, y * sliceSize * -1f, containerView.Bounds.Width, containerView.Bounds.Height), RasterizationScale = fromViewSnapshot.CurrentScale, Contents = fromViewSnapshot.CGImage }; var toContentLayer = new CALayer(); toContentLayer.Frame = new CGRect(x * sliceSize * -1f, y * sliceSize * -1f, containerView.Bounds.Width, containerView.Bounds.Height); CoreFoundation.DispatchQueue.MainQueue.DispatchAsync(() => { bool wereActionDisabled = CATransaction.DisableActions; CATransaction.DisableActions = true; toContentLayer.RasterizationScale = toViewSnapshot.CurrentScale; toContentLayer.Contents = toViewSnapshot.CGImage; CATransaction.DisableActions = wereActionDisabled; }); var toCheckboardSquareView = new UIView { Frame = new CGRect(x * sliceSize, y * sliceSize, sliceSize, sliceSize), Opaque = false }; toCheckboardSquareView.Layer.MasksToBounds = true; toCheckboardSquareView.Layer.DoubleSided = false; toCheckboardSquareView.Layer.Transform = CATransform3D.MakeRotation(NMath.PI, 0f, 1f, 0f); toCheckboardSquareView.Layer.AddSublayer(toContentLayer); var fromCheckboardSquareView = new UIView { Frame = new CGRect(x * sliceSize, y * sliceSize, sliceSize, sliceSize), Opaque = false }; fromCheckboardSquareView.Layer.MasksToBounds = true; fromCheckboardSquareView.Layer.DoubleSided = false; fromCheckboardSquareView.Layer.Transform = CATransform3D.Identity; fromCheckboardSquareView.Layer.AddSublayer(fromContentLayer); transitionContainer.AddSubview(toCheckboardSquareView); transitionContainer.AddSubview(fromCheckboardSquareView); } } int sliceAnimationsPending = 0; for (int y = 0; y < verticalSlices; y++) { for (int x = 0; x < horizontalSileces; x++) { double toIndex = y * horizontalSileces * 2f + (x * 2); UIView toCheckboardSquareView = transitionContainer.Subviews[(int)toIndex]; double fromIndex = y * horizontalSileces * 2f + (x * 2 + 1); UIView fromCheckboardSquareView = transitionContainer.Subviews[(int)fromIndex]; CGVector sliceOriginVector; if (isPush) { sliceOriginVector = new CGVector(fromCheckboardSquareView.Frame.GetMinX() - transitionContainer.Bounds.GetMinX(), fromCheckboardSquareView.Frame.GetMinY() - transitionContainer.Bounds.GetMinY()); } else { sliceOriginVector = new CGVector(fromCheckboardSquareView.Frame.GetMaxX() - transitionContainer.Bounds.GetMaxX(), fromCheckboardSquareView.Frame.GetMaxY() - transitionContainer.Bounds.GetMaxY()); } // Project sliceOriginVector onto transitionVector. nfloat dot = sliceOriginVector.dx * transitionVector.dx + sliceOriginVector.dy * transitionVector.dy; var projection = new CGVector(transitionUnitVector.dx * dot / transitionVectorLength, transitionUnitVector.dy * dot / transitionVectorLength); // Compute the length of the projection. var projectionLength = NMath.Sqrt(projection.dx * projection.dx + projection.dy * projection.dy); double startTime = projectionLength / (transitionVectorLength + transitionSpacing) * transitionDuration; double duration = ((projectionLength + transitionSpacing) / (transitionVectorLength + transitionSpacing) * transitionDuration) - startTime; sliceAnimationsPending++; UIView.Animate(duration, startTime, UIViewAnimationOptions.TransitionNone, () => { toCheckboardSquareView.Layer.Transform = CATransform3D.Identity; fromCheckboardSquareView.Layer.Transform = CATransform3D.MakeRotation(NMath.PI, 0f, 1f, 0f); }, () => { if (--sliceAnimationsPending == 0) { bool wasCancelled = transitionContext.TransitionWasCancelled; transitionContainer.RemoveFromSuperview(); transitionContext.CompleteTransition(!wasCancelled); } } ); } } }
public override void AnimateTransition(IUIViewControllerContextTransitioning transitionContext) { var fromViewController = transitionContext.GetViewControllerForKey(UITransitionContext.FromViewControllerKey); var toViewController = transitionContext.GetViewControllerForKey(UITransitionContext.ToViewControllerKey); UIView containerView = transitionContext.ContainerView; var fromView = transitionContext.GetViewFor(UITransitionContext.FromViewKey); var toView = transitionContext.GetViewFor(UITransitionContext.ToViewKey); bool isPresenting = (toViewController.PresentingViewController == fromViewController); var fromFrame = transitionContext.GetInitialFrameForViewController(fromViewController); var toFrame = transitionContext.GetFinalFrameForViewController(toViewController); var offset = new CGVector(0f, 0f); switch (targetEdge) { case UIRectEdge.Top: offset = new CGVector(0f, 1f); break; case UIRectEdge.Bottom: offset = new CGVector(0f, -1f); break; case UIRectEdge.Left: offset = new CGVector(1f, 0f); break; case UIRectEdge.Right: offset = new CGVector(-1, 0); break; default: offset = new CGVector(0f, 0f); // TODO: error break; } if (isPresenting) { fromView.Frame = fromFrame; toView.Frame = new CGRect(toFrame.X + (toFrame.Size.Width * offset.dx * -1), toFrame.Y + (toFrame.Size.Height * offset.dy * -1), toFrame.Size.Width, toFrame.Size.Height); } else { fromView.Frame = fromFrame; toView.Frame = toFrame; } if (isPresenting) { containerView.AddSubview(toView); } else { containerView.InsertSubviewBelow(toView, fromView); } var duration = TransitionDuration(transitionContext); UIView.Animate(duration, 0, UIViewAnimationOptions.TransitionNone, () => { toView.Frame = isPresenting ? toFrame : new CGRect(fromFrame.X + (fromFrame.Size.Width * offset.dx), fromFrame.Y + (fromFrame.Size.Height * offset.dy), fromFrame.Size.Width, fromFrame.Size.Height); }, () => { bool wasCancel = transitionContext.TransitionWasCancelled; if (wasCancel) { toView.RemoveFromSuperview(); transitionContext.CompleteTransition(!wasCancel); } }); }