Skip to content
This repository
branch: master
Miguel de Icaza April 07, 2011
file 130 lines (108 sloc) 3.848 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
using System;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Drawing;

namespace MonoTouch.Dialog
{

/// <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);
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;

UIColor background = Enabled ? pressed ? HighlightedColor : NormalColor : DisabledColor;
float alpha = 1;

CGPath container;
container = GraphicsUtil.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 = GraphicsUtil.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 = GraphicsUtil.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 (2);
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);
}
}
}
}
}

Something went wrong with that request. Please try again.