'Translation animation between views on xamarin.ios
I'm trying to develop on xamari.ios, the animation consists in the translation of the View, when the swipe gesture is detected, the animation follows the finger and when it is lifted from the screen, a second view appears. To better explain, the animation of the iOS, iMessage, and Whatsapp settings (when you go back)
I'm trying to manage the views manually and programming their movement, but I find it difficult when I have to connect the second view to the destination one .. with an existing view present in the storyboards ..
public void AnimazioneSwipe(UIView view1, NSObject view2)
{
float displayW = (float)DeviceDisplay.MainDisplayInfo.Width; //Ottengo le informazioni del display corrente
CAKeyFrameAnimation animation = (CAKeyFrameAnimation)CAKeyFrameAnimation.FromKeyPath("transform.translation.x");
animation.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.Linear);
animation.Duration = 1;
animation.Values = new NSObject[]
{
NSNumber.FromFloat(0),
NSNumber.FromFloat(-displayW)
};
view1.Layer.AddAnimation(animation, "shake");
/* CAKeyFrameAnimation animation2 = (CAKeyFrameAnimation)CAKeyFrameAnimation.FromKeyPath("transform.translation.x");
animation2.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.Linear);
animation2.Duration = 1;
animation2.Values = new NSObject[]
{
NSNumber.FromFloat(880),
NSNumber.FromFloat(0)
};
view2.Layer.AddAnimation(animation2, "shake");*/
}
Solution 1:[1]
I solved with this code, it is still to be perfected...
/*Riproduco l'animazione di classica di iOS dove se si scorre nel bordo sinistro, in tempo reale la View si anima
*e si sposta indirizzando verso un altra pagina che in questo caso e la pagina precedente!
View: La view da animare
VCD: ViewController di destinazione*/
public void SlideAnimation(UIView View, string VCD)
{
var interactiveTransitionRecognizer = new UIScreenEdgePanGestureRecognizer();
interactiveTransitionRecognizer.AddTarget(() => InteractiveTransitionRecognizerActionWithoutIndex(interactiveTransitionRecognizer, View, VCD));
interactiveTransitionRecognizer.Edges = UIRectEdge.Left;
View.AddGestureRecognizer(interactiveTransitionRecognizer);
}
//Metodo di appoggio
private void InteractiveTransitionRecognizerActionWithoutIndex(UIScreenEdgePanGestureRecognizer sender, UIView View, string x)
{
//Contiene un valore numerico che varia in base allo stato della gesture
var percento = sender.TranslationInView(View).X * 100 / sender.View.Bounds.Size.Width;
//Quando la gesture rileva una variazione
if (sender.State == UIGestureRecognizerState.Changed)
{
var minTransform = CGAffineTransform.MakeTranslation(sender.TranslationInView(View).X, 0); //*2 = piu rapido
var maxTransform = CGAffineTransform.MakeTranslation(sender.TranslationInView(View).X, 0);
View.Transform = true ? minTransform : maxTransform;
UIView.Animate(0.1, 0, UIViewAnimationOptions.CurveEaseInOut,
() =>
{
View.Transform = true ? maxTransform : minTransform;
},
null
);
}
//Quando la gesture termina, si alza il dito o limite schermo
else if (sender.State == UIGestureRecognizerState.Ended)
{
if (percento <= 40)
{
//Annullo l'animazion e torna in posizione
var minTransform = CGAffineTransform.MakeTranslation(sender.TranslationInView(View).X, 0);
var maxTransform = CGAffineTransform.MakeIdentity();
View.Transform = true ? minTransform : maxTransform;
UIView.Animate(0.1, 0, UIViewAnimationOptions.CurveEaseInOut,
() =>
{
View.Transform = true ? maxTransform : minTransform;
},
null
);
}
else
{
//La gesture รจ conclusa quindi eseguo il seguente codice
//Feedback haptico
var impact = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Soft);
impact.Prepare();
impact.ImpactOccurred();
//Indirizzo
var storyboard = UIStoryboard.FromName("Main", null);
var viewController = storyboard.InstantiateViewController(x); // Your view controller here
UIApplication.SharedApplication.KeyWindow.RootViewController = viewController;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alessandro Ledda |