Created
November 13, 2011 18:57
-
-
Save nicwise/1362490 to your computer and use it in GitHub Desktop.
glass button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Section loginSection = new Section(); | |
var loginButton = new BTButtonElement("Settings", BTButtonElement.Green); | |
loginButton.Tapped += delegate { | |
AppLauncher.Instance.SetupLoginDialog(this.ParentViewController, true, delegate { | |
DataSource.Instance.RefreshRegisteredViewControllers(); | |
}); | |
}; | |
loginSection.Add(loginButton); | |
public class BTButtonElement : Element, IElementSizing | |
{ | |
static int count; | |
NSString key; | |
protected UIView View; | |
public CellFlags Flags; | |
public enum CellFlags { | |
Transparent = 1, | |
DisableSelection = 2 | |
} | |
public static UIColor Red = new UIColor (0.55f, 0.04f, 0.02f, 1); | |
public static UIColor Green = new UIColor (0.04f, 0.55f, 0.02f, 1); | |
public event NSAction Tapped; | |
/// <summary> | |
/// Constructor | |
/// </summary> | |
/// <param name="caption"> | |
/// The caption, only used for RootElements that might want to summarize results | |
/// </param> | |
/// <param name="view"> | |
/// The view to display | |
/// </param> | |
/// <param name="transparent"> | |
/// If this is set, then the view is responsible for painting the entire area, | |
/// otherwise the default cell paint code will be used. | |
/// </param> | |
public BTButtonElement (string caption, UIColor color) : base (caption) | |
{ | |
this.Button = new GlassButton(new RectangleF(0,0,300, 50)) { | |
Font = UIFont.BoldSystemFontOfSize (18), | |
NormalColor = color | |
}; | |
Button.SetTitle(caption, UIControlState.Normal); | |
Button.Tapped += delegate(GlassButton obj) { | |
if (Tapped != null) { | |
Tapped(); | |
} | |
}; | |
this.Flags = CellFlags.Transparent; | |
key = new NSString ("BTButtonElement" + count++); | |
} | |
GlassButton Button; | |
public override UITableViewCell GetCell (UITableView tv) | |
{ | |
var cell = tv.DequeueReusableCell (key); | |
if (cell == null){ | |
cell = new UITableViewCell (UITableViewCellStyle.Default, key); | |
if ((Flags & CellFlags.Transparent) != 0){ | |
cell.BackgroundColor = UIColor.Clear; | |
// | |
// This trick is necessary to keep the background clear, otherwise | |
// it gets painted as black | |
// | |
cell.BackgroundView = new UIView (RectangleF.Empty) { | |
BackgroundColor = UIColor.Clear | |
}; | |
} | |
if ((Flags & CellFlags.DisableSelection) != 0) | |
cell.SelectionStyle = UITableViewCellSelectionStyle.None; | |
cell.ContentView.AddSubview (Button); | |
} | |
return cell; | |
} | |
public float GetHeight (UITableView tableView, NSIndexPath indexPath) | |
{ | |
return Button.Bounds.Height; | |
} | |
protected override void Dispose (bool disposing) | |
{ | |
base.Dispose (disposing); | |
//if (disposing){ | |
// View.Dispose (); | |
// View = null; | |
//} | |
} | |
} | |
//------------------------------ | |
// glass button came from MT.D | |
/// <summary> | |
/// GlassButton is a glossy/glass button. User code can use either | |
/// targets or can subscribe to the Tapped event. Colors are customized | |
/// by asssigning to the NormalColor, HighlightedColor and DisabledColor | |
/// properties | |
/// </summary> | |
public class GlassButton : UIButton { | |
bool pressed; | |
public UIColor NormalColor, HighlightedColor, DisabledColor; | |
/// <summary> | |
/// Invoked when the user touches | |
/// </summary> | |
public event Action<GlassButton> Tapped; | |
/// <summary> | |
/// Creates a new instance of the GlassButton using the specified dimensions | |
/// </summary> | |
public GlassButton (RectangleF frame) : base (frame) | |
{ | |
NormalColor = new UIColor (0.55f, 0.04f, 0.02f, 1); | |
//NormalColor = new UIColor (0.04f, 0.55f, 0.02f, 1); | |
HighlightedColor = UIColor.Black; | |
DisabledColor = UIColor.Gray; | |
} | |
public void SetGreen() | |
{ | |
NormalColor = new UIColor (0.04f, 0.55f, 0.02f, 1); | |
HighlightedColor = UIColor.Black; | |
DisabledColor = UIColor.Gray; | |
} | |
/// <summary> | |
/// Whether the button is rendered enabled or not. | |
/// </summary> | |
public override bool Enabled { | |
get { | |
return base.Enabled; | |
} | |
set { | |
base.Enabled = value; | |
SetNeedsDisplay (); | |
} | |
} | |
public override bool BeginTracking (UITouch uitouch, UIEvent uievent) | |
{ | |
SetNeedsDisplay (); | |
pressed = true; | |
return base.BeginTracking (uitouch, uievent); | |
} | |
public override void EndTracking (UITouch uitouch, UIEvent uievent) | |
{ | |
if (pressed && Enabled){ | |
if (Tapped != null) | |
Tapped (this); | |
} | |
pressed = false; | |
SetNeedsDisplay (); | |
base.EndTracking (uitouch, uievent); | |
} | |
public override bool ContinueTracking (UITouch uitouch, UIEvent uievent) | |
{ | |
var touch = uievent.AllTouches.AnyObject as UITouch; | |
if (Bounds.Contains (touch.LocationInView (this))) | |
pressed = true; | |
else | |
pressed = false; | |
return base.ContinueTracking (uitouch, uievent); | |
} | |
public override void Draw (RectangleF rect) | |
{ | |
var context = UIGraphics.GetCurrentContext (); | |
var bounds = Bounds; | |
SetTitleColor(NormalColor == UIColor.White ? UIColor.Black : UIColor.White,UIControlState.Normal); | |
UIColor background = Enabled ? pressed ? HighlightedColor : NormalColor : DisabledColor; | |
float alpha = 1; | |
CGPath container; | |
container = MakeRoundedRectPath (bounds, 14); | |
context.AddPath (container); | |
context.Clip (); | |
using (var cs = CGColorSpace.CreateDeviceRGB ()){ | |
var topCenter = new PointF (bounds.GetMidX (), 0); | |
var midCenter = new PointF (bounds.GetMidX (), bounds.GetMidY ()); | |
var bottomCenter = new PointF (bounds.GetMidX (), bounds.GetMaxY ()); | |
using (var gradient = new CGGradient (cs, new float [] { 0.23f, 0.23f, 0.23f, alpha, 0.47f, 0.47f, 0.47f, alpha }, new float [] {0, 1})){ | |
//context.DrawLinearGradient (gradient, topCenter, bottomCenter, 0); | |
} | |
container = MakeRoundedRectPath (bounds.Inset (1, 1), 13); | |
context.AddPath (container); | |
context.Clip (); | |
using (var gradient = new CGGradient (cs, new float [] { 0.05f, 0.05f, 0.05f, alpha, 0.15f, 0.15f, 0.15f, alpha}, new float [] {0, 1})){ | |
//context.DrawLinearGradient (gradient, topCenter, bottomCenter, 0); | |
} | |
var nb = bounds.Inset (4, 4); | |
container = MakeRoundedRectPath (nb, 10); | |
context.AddPath (container); | |
context.Clip (); | |
background.SetFill (); | |
context.FillRect (nb); | |
using (var gradient = new CGGradient (cs, new float [] { 1, 1, 1, .35f, 1, 1, 1, 0.06f }, new float [] { 0, 1 })){ | |
context.DrawLinearGradient (gradient, topCenter, midCenter, 0); | |
} | |
context.SetLineWidth (1); | |
context.AddPath (container); | |
context.ReplacePathWithStrokedPath (); | |
context.Clip (); | |
using (var gradient = new CGGradient (cs, new float [] { 1, 1, 1, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f }, new float [] { 0, 1 })){ | |
context.DrawLinearGradient (gradient, topCenter, bottomCenter, 0); | |
} | |
} | |
} | |
public CGPath MakeRoundedRectPath (RectangleF rect, float radius) | |
{ | |
float minx = rect.Left; | |
float midx = rect.Left + (rect.Width)/2; | |
float maxx = rect.Right; | |
float miny = rect.Top; | |
float midy = rect.Y+rect.Size.Height/2; | |
float maxy = rect.Bottom; | |
var path = new CGPath (); | |
path.MoveToPoint (minx, midy); | |
path.AddArcToPoint (minx, miny, midx, miny, radius); | |
path.AddArcToPoint (maxx, miny, maxx, midy, radius); | |
path.AddArcToPoint (maxx, maxy, midx, maxy, radius); | |
path.AddArcToPoint (minx, maxy, minx, midy, radius); | |
path.CloseSubpath (); | |
return path; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment