Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Xamarin.iOS view common animations
public static void Fade (this UIView view, bool isIn, double duration = 0.3, Action onFinished = null)
{
var minAlpha = (nfloat)0.0f;
var maxAlpha = (nfloat)1.0f;
view.Alpha = isIn ? minAlpha : maxAlpha;
view.Transform = CGAffineTransform.MakeIdentity ();
UIView.Animate (duration, 0, UIViewAnimationOptions.CurveEaseInOut,
() => {
view.Alpha = isIn ? maxAlpha : minAlpha;
},
onFinished
);
}
public static void FlipHorizontaly (this UIView view, bool isIn, double duration = 0.3, Action onFinished = null)
{
var m34 = (nfloat)(-1 * 0.001);
var minAlpha = (nfloat)0.0f;
var maxAlpha = (nfloat)1.0f;
view.Alpha = (nfloat)1.0;
var minTransform = CATransform3D.Identity;
minTransform.m34 = m34;
minTransform = minTransform.Rotate ((nfloat)((isIn ? 1 : -1) * Math.PI * 0.5), (nfloat)0.0f, (nfloat)1.0f, (nfloat)0.0f);
var maxTransform = CATransform3D.Identity;
maxTransform.m34 = m34;
view.Alpha = isIn ? minAlpha : maxAlpha;
view.Layer.Transform = isIn ? minTransform : maxTransform;
UIView.Animate (duration, 0, UIViewAnimationOptions.CurveEaseInOut,
() => {
view.Layer.AnchorPoint = new CGPoint ((nfloat)0.5, (nfloat)0.5f);
view.Layer.Transform = isIn ? maxTransform : minTransform;
view.Alpha = isIn ? maxAlpha : minAlpha;
},
onFinished
);
}
public static void FlipVerticaly (this UIView view, bool isIn, double duration = 0.3, Action onFinished = null)
{
var m34 = (nfloat)(-1 * 0.001);
var minAlpha = (nfloat)0.0f;
var maxAlpha = (nfloat)1.0f;
var minTransform = CATransform3D.Identity;
minTransform.m34 = m34;
minTransform = minTransform.Rotate ((nfloat)((isIn ? 1 : -1) * Math.PI * 0.5), (nfloat)1.0f, (nfloat)0.0f, (nfloat)0.0f);
var maxTransform = CATransform3D.Identity;
maxTransform.m34 = m34;
view.Alpha = isIn ? minAlpha : maxAlpha;
view.Layer.Transform = isIn ? minTransform : maxTransform;
UIView.Animate (duration, 0, UIViewAnimationOptions.CurveEaseInOut,
() => {
view.Layer.AnchorPoint = new CGPoint ((nfloat)0.5, (nfloat)0.5f);
view.Layer.Transform = isIn ? maxTransform : minTransform;
view.Alpha = isIn ? maxAlpha : minAlpha;
},
onFinished
);
}
public static void Rotate (this UIView view, bool isIn, bool fromLeft = true, double duration = 0.3, Action onFinished = null)
{
var minAlpha = (nfloat)0.0f;
var maxAlpha = (nfloat)1.0f;
var minTransform = CGAffineTransform.MakeRotation ((nfloat)((fromLeft ? -1 : 1) * 720));
var maxTransform = CGAffineTransform.MakeRotation ((nfloat)0.0);
view.Alpha = isIn ? minAlpha : maxAlpha;
view.Transform = isIn ? minTransform : maxTransform;
UIView.Animate (duration, 0, UIViewAnimationOptions.CurveEaseInOut,
() => {
view.Alpha = isIn ? maxAlpha : minAlpha;
view.Transform = isIn ? maxTransform : minTransform;
},
onFinished
);
}
public static void Scale (this UIView view, bool isIn, double duration = 0.3, Action onFinished = null)
{
var minAlpha = (nfloat)0.0f;
var maxAlpha = (nfloat)1.0f;
var minTransform = CGAffineTransform.MakeScale ((nfloat)0.1, (nfloat)0.1);
var maxTransform = CGAffineTransform.MakeScale ((nfloat)1, (nfloat)1);
view.Alpha = isIn ? minAlpha : maxAlpha;
view.Transform = isIn ? minTransform : maxTransform;
UIView.Animate (duration, 0, UIViewAnimationOptions.CurveEaseInOut,
() => {
view.Alpha = isIn ? maxAlpha : minAlpha;
view.Transform = isIn ? maxTransform : minTransform;
},
onFinished
);
}
public static void SlideHorizontaly (this UIView view, bool isIn, bool fromLeft, double duration = 0.3, Action onFinished = null)
{
var minAlpha = (nfloat)0.0f;
var maxAlpha = (nfloat)1.0f;
var minTransform = CGAffineTransform.MakeTranslation ((fromLeft ? -1 : 1) * view.Bounds.Width, 0);
var maxTransform = CGAffineTransform.MakeIdentity ();
view.Alpha = isIn ? minAlpha : maxAlpha;
view.Transform = isIn ? minTransform : maxTransform;
UIView.Animate (duration, 0, UIViewAnimationOptions.CurveEaseInOut,
() => {
view.Alpha = isIn ? maxAlpha : minAlpha;
view.Transform = isIn ? maxTransform : minTransform;
},
onFinished
);
}
public static void SlideVerticaly (this UIView view, bool isIn, bool fromTop, double duration = 0.3, Action onFinished = null)
{
var minAlpha = (nfloat)0.0f;
var maxAlpha = (nfloat)1.0f;
var minTransform = CGAffineTransform.MakeTranslation (0, (fromTop ? -1 : 1) * view.Bounds.Height);
var maxTransform = CGAffineTransform.MakeIdentity ();
view.Alpha = isIn ? minAlpha : maxAlpha;
view.Transform = isIn ? minTransform : maxTransform;
UIView.Animate (duration, 0, UIViewAnimationOptions.CurveEaseInOut,
() => {
view.Alpha = isIn ? maxAlpha : minAlpha;
view.Transform = isIn ? maxTransform : minTransform;
},
onFinished
);
}
public static void Zoom (this UIView view, bool isIn, double duration = 0.3, Action onFinished = null)
{
var minAlpha = (nfloat)0.0f;
var maxAlpha = (nfloat)1.0f;
var minTransform = CGAffineTransform.MakeScale ((nfloat)2.0, (nfloat)2.0);
var maxTransform = CGAffineTransform.MakeScale ((nfloat)1, (nfloat)1);
view.Alpha = isIn ? minAlpha : maxAlpha;
view.Transform = isIn ? minTransform : maxTransform;
UIView.Animate (duration, 0, UIViewAnimationOptions.CurveEaseInOut,
() => {
view.Alpha = isIn ? maxAlpha : minAlpha;
view.Transform = isIn ? maxTransform : minTransform;
},
onFinished
);
}
//Some examples from below animation gists
this.view = new UIView (new CGRect (50, 50, 70, 100));
this.view.BackgroundColor = UIColor.Red;
this.View.AddSubview (this.view);
this.animations = new Dictionary<string, Action> () {
{ "FadeIn", () => view.Fade (true) },
{ "FadeOut", () => view.Fade (false) },
{ "RotateIn", () => view.Rotate (true) },
{ "RotateOut", () => view.Rotate (false) },
{ "FlipInVerticaly", () => view.FlipVerticaly (true) },
{ "FlipOutVerticaly", () => view.FlipVerticaly (false) },
{ "FlipInHorizontaly", () => view.FlipHorizontaly (true) },
{ "FlipOutHorizontaly", () => view.FlipHorizontaly (false) },
{ "SlideInFromTop", () => view.SlideVerticaly (true, true) },
{ "SlideInFromBottom", () => view.SlideVerticaly (true, false) },
{ "SlideInFromLeft", () => view.SlideHorizontaly (true, true) },
{ "SlideInFromRight", () => view.SlideHorizontaly (true, false) },
{ "SlideOutFromTop", () => view.SlideVerticaly (false, true) },
{ "SlideOutFromBottom", () => view.SlideVerticaly (false, false) },
{ "SlideOutFromLeft", () => view.SlideHorizontaly (false, true) },
{ "SlideOutFromRight", () => view.SlideHorizontaly (false, false) },
{ "ZoomIn", () => view.Zoom (true) },
{ "ZoomOut", () => view.Zoom (false) },
{ "ScaleIn", () => view.Scale (true) },
{ "ScaleOut", () => view.Scale (false) },
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.