|
| 1 | +using System; |
| 2 | +using System.Drawing; |
| 3 | + |
| 4 | +using MonoTouch.CoreAnimation; |
| 5 | +using MonoTouch.CoreGraphics; |
| 6 | +using MonoTouch.Foundation; |
| 7 | +using MonoTouch.UIKit; |
| 8 | + |
| 9 | +namespace CustomPropertyAnimation |
| 10 | +{ |
| 11 | + [Register ("AppDelegate")] |
| 12 | + public partial class AppDelegate : UIApplicationDelegate |
| 13 | + { |
| 14 | + UIWindow window; |
| 15 | + UIViewController vc; |
| 16 | + CircleLayer testLayer; |
| 17 | + CABasicAnimation radiusAnimation; |
| 18 | + CABasicAnimation thicknessAnimation; |
| 19 | + CABasicAnimation colorAnimation; |
| 20 | + |
| 21 | + public override bool FinishedLaunching (UIApplication app, NSDictionary options) |
| 22 | + { |
| 23 | + // create a new window instance based on the screen size |
| 24 | + window = new UIWindow (UIScreen.MainScreen.Bounds); |
| 25 | + |
| 26 | + vc = new UIViewController (); |
| 27 | + vc.View.BackgroundColor = UIColor.Black; |
| 28 | + testLayer = new CircleLayer(); |
| 29 | + testLayer.Color = UIColor.Green.CGColor; |
| 30 | + testLayer.Thickness = 19f; |
| 31 | + testLayer.Radius = 60f; |
| 32 | + |
| 33 | + testLayer.Frame = vc.View.Layer.Bounds; |
| 34 | + vc.View.Layer.AddSublayer(testLayer); |
| 35 | + |
| 36 | + testLayer.SetNeedsDisplay(); |
| 37 | + |
| 38 | + radiusAnimation = CABasicAnimation.FromKeyPath ("radius"); |
| 39 | + radiusAnimation.Duration = 3; |
| 40 | + radiusAnimation.To = NSNumber.FromDouble (120); |
| 41 | + radiusAnimation.RepeatCount = 1000; |
| 42 | + |
| 43 | + thicknessAnimation = CABasicAnimation.FromKeyPath ("thickness"); |
| 44 | + thicknessAnimation.Duration = 2; |
| 45 | + thicknessAnimation.From = NSNumber.FromDouble (5); |
| 46 | + thicknessAnimation.To = NSNumber.FromDouble (38); |
| 47 | + thicknessAnimation.RepeatCount = 1000; |
| 48 | + |
| 49 | + colorAnimation = CABasicAnimation.FromKeyPath ("circleColor"); |
| 50 | + colorAnimation.Duration = 4; |
| 51 | + colorAnimation.To = new NSObject (UIColor.Blue.CGColor.Handle); |
| 52 | + colorAnimation.RepeatCount = 1000; |
| 53 | + |
| 54 | + testLayer.AddAnimation (radiusAnimation, "radiusAnimation"); |
| 55 | + testLayer.AddAnimation (thicknessAnimation, "thicknessAnimation"); |
| 56 | + testLayer.AddAnimation (colorAnimation, "colorAnimation"); |
| 57 | + |
| 58 | + window.RootViewController = vc; |
| 59 | + // make the window visible |
| 60 | + window.MakeKeyAndVisible (); |
| 61 | + |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + // This is the main entry point of the application. |
| 66 | + static void Main (string[] args) |
| 67 | + { |
| 68 | + // if you want to use a different Application Delegate class from "AppDelegate" |
| 69 | + // you can specify it here. |
| 70 | + UIApplication.Main (args, null, "AppDelegate"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public class CircleLayer : CALayer |
| 75 | + { |
| 76 | + public CircleLayer () |
| 77 | + { |
| 78 | + } |
| 79 | + |
| 80 | + [Export ("initWithLayer:")] |
| 81 | + public CircleLayer (CALayer other) |
| 82 | + : base (other) |
| 83 | + { |
| 84 | + } |
| 85 | + |
| 86 | + public override void Clone (CALayer other) |
| 87 | + { |
| 88 | + CircleLayer o = (CircleLayer) other; |
| 89 | + Radius = o.Radius; |
| 90 | + Color = o.Color; |
| 91 | + Thickness = o.Thickness; |
| 92 | + base.Clone (other); |
| 93 | + } |
| 94 | + |
| 95 | + [Export ("radius")] |
| 96 | + public double Radius { get; set; } |
| 97 | + |
| 98 | + [Export ("thickness")] |
| 99 | + public double Thickness { get; set; } |
| 100 | + |
| 101 | + [Export ("circleColor")] |
| 102 | + public CGColor Color { get; set; } |
| 103 | + |
| 104 | + [Export ("needsDisplayForKey:")] |
| 105 | + static bool NeedsDisplayForKey (NSString key) |
| 106 | + { |
| 107 | + switch (key.ToString ()) { |
| 108 | + case "radius": |
| 109 | + case "thickness": |
| 110 | + case "circleColor": |
| 111 | + return true; |
| 112 | + default: |
| 113 | + return CALayer.NeedsDisplayForKey (key); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public override void DrawInContext (CGContext context) |
| 118 | + { |
| 119 | + base.DrawInContext (context); |
| 120 | + |
| 121 | + // Console.WriteLine ("DrawInContext Radius: {0} Thickness: {1} Color: {2}", Radius, Thickness, Color); |
| 122 | + |
| 123 | + PointF centerPoint = new PointF (this.Bounds.Width / 2, this.Bounds.Height / 2); |
| 124 | + CGColor glowColor = new UIColor (Color).ColorWithAlpha (0.85f).CGColor; |
| 125 | + double innerRadius = (Radius - Thickness) > 0 ? Radius - Thickness : 0; |
| 126 | + |
| 127 | + // Outer circle |
| 128 | + context.AddEllipseInRect (new RectangleF (centerPoint.X - (float) Radius, |
| 129 | + centerPoint.Y - (float) Radius, |
| 130 | + (float) Radius * 2, |
| 131 | + (float) Radius * 2)); |
| 132 | + // Inner circle |
| 133 | + context.AddEllipseInRect (new RectangleF (centerPoint.X - (float) innerRadius, |
| 134 | + centerPoint.Y - (float) innerRadius, |
| 135 | + (float) innerRadius * 2, |
| 136 | + (float) innerRadius * 2)); |
| 137 | + |
| 138 | + // Fill in circle |
| 139 | + context.SetFillColor (Color); |
| 140 | + context.SetShadowWithColor (SizeF.Empty, 10.0f, glowColor); |
| 141 | + context.EOFillPath(); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments