Skip to content

Instantly share code, notes, and snippets.

@dvdsgl
Created December 18, 2012 06:37
Show Gist options
  • Save dvdsgl/4325599 to your computer and use it in GitHub Desktop.
Save dvdsgl/4325599 to your computer and use it in GitHub Desktop.
Extension method for succinct RectangleF mutation expressions.
public static class RectangleExtensions
{
public static RectangleF With (this RectangleF self,
float? X = null, float? Y = null,
float? Width = null, float? Height = null)
{
return new RectangleF (X ?? self.X, Y ?? self.Y, Width ?? self.Width, Height ?? self.Height);
}
static T Id<T> (T t) { return t; }
public static RectangleF With (this RectangleF self,
Func<float,float> X = null, Func<float,float> Y = null,
Func<float,float> Width = null, Func<float,float> Height = null)
{
return new RectangleF ((X ?? Id) (self.X), (Y ?? Id) (self.Y),
(Width ?? Id) (self.Width), (Height ?? Id) (self.Height));
}
}
// Demo 1
UIView.Animate (0.5, () => {
View.Frame = View.Frame.With (Width: 200, Y: 50);
});
// Demo 2
UIView.Animate (0.5, () => {
View.Frame = View.Frame.With (Y: y => y - 100);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment