Skip to content

Instantly share code, notes, and snippets.

@philcockfield
Created July 10, 2012 09:48
Show Gist options
  • Save philcockfield/3082372 to your computer and use it in GitHub Desktop.
Save philcockfield/3082372 to your computer and use it in GitHub Desktop.
DatePicker
using System;
using MonoTouch.UIKit;
using System.Drawing;
namespace Core.Controls
{
public class DatePicker
{
#region Head
public event EventHandler<EventArgs> Changed;
UIView within;
// Constructor.
public DatePicker(UIView within)
{
// Setup initial conditions.
this.within = within;
Control = new UIDatePicker(RectangleF.Empty){ AutoresizingMask = UIViewAutoresizing.FlexibleWidth };
Duration = 0.2;
// Wire up events.
Control.AddTarget((s, e) => {
if (Changed != null) Changed(this, new EventArgs());
},
UIControlEvent.ValueChanged);
}
#endregion
#region Properties - Public
public UIDatePicker Control { get; private set; }
public double Duration { get; set; }
public bool IsShowing { get; private set; }
public bool IsAnimating { get; private set; }
public DateTime Date
{
get{ return Control.Date; }
set{ Control.Date = value; }
}
#endregion
#region Methods - Public
public bool Toggle(Action onComplete = null)
{
if (IsShowing) {
return Hide(onComplete);
} else {
return Show(onComplete);
}
}
public bool Show(Action onComplete = null)
{
// Setup initial conditions.
if(IsShowing || IsAnimating) return false;
// Insert the date-picker off the bottom of the screen.
Control.Frame = offScreenBounds();
within.AddSubview(Control);
// Animate on screen.
IsAnimating = true;
UIView.AnimateNotify(
Duration,
animation:() => {
Control.Frame = onScreenBounds();
},
completion: (finished) => {
// Done.
IsAnimating = false;
IsShowing = true;
if (onComplete != null) onComplete();
});
// Finish up.
return true;
}
public bool Hide(Action onComplete = null)
{
// Setup initial conditions.
if(!IsShowing || IsAnimating) return false;
// Animate off screen.
IsAnimating = true;
UIView.AnimateNotify(
Duration,
animation:() => {
Control.Frame = offScreenBounds();
},
completion: (finished) => {
// Done.
IsAnimating = false;
IsShowing = false;
Control.RemoveFromSuperview();
if (onComplete != null) onComplete();
});
// Finish up.
return true;
}
#endregion
#region Internal
private SizeF size(){ return new SizeF(within.Frame.Width, Control.Frame.Height); }
private RectangleF offScreenBounds()
{
return new RectangleF (new PointF(0, within.Frame.Height), size());
}
private RectangleF onScreenBounds()
{
var top = within.Frame.Height - Control.Frame.Height;
return new RectangleF (new PointF(0, top), size());
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment