Created
October 25, 2017 16:16
-
-
Save chamons/1aa1beef5183141b9c67b4312efeb670 to your computer and use it in GitHub Desktop.
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
using System; | |
using AppKit; | |
using CoreAnimation; | |
using CoreGraphics; | |
using Foundation; | |
namespace CustomizeAnimationTest2 | |
{ | |
public partial class ViewController : NSViewController | |
{ | |
public ViewController (IntPtr handle) : base (handle) | |
{ | |
} | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
MyView view = new MyView (new CGRect (0, 0, 250, 250)); | |
View.AddSubview (view); | |
} | |
} | |
public class MyView : NSView | |
{ | |
NSColor color; | |
NSBezierPath path; | |
public MyView (IntPtr p) : base (p) | |
{ | |
} | |
public MyView (CGRect rect) : base (rect) | |
{ | |
color = NSColor.Blue; | |
path = new NSBezierPath (); | |
path.MoveTo (Bounds.Location); | |
path.LineTo (new CGPoint (Bounds.GetMaxX (), Bounds.GetMaxY ())); | |
DrawLineWidth = 1; | |
WantsLayer = true; | |
Layer.BackgroundColor = NSColor.Gray.CGColor; | |
} | |
NSNumber _drawnLineWidth; | |
public NSNumber DrawLineWidth | |
{ | |
[Export ("DrawLineWidth")] | |
get | |
{ | |
return _drawnLineWidth; | |
} | |
[Export ("setDrawLineWidth:")] | |
set | |
{ | |
WillChangeValue ("DrawLineWidth"); | |
_drawnLineWidth = value; | |
NeedsDisplay = true; | |
DidChangeValue ("DrawLineWidth"); | |
} | |
} | |
public override bool AcceptsFirstResponder () => true; | |
public override void MouseDown (NSEvent theEvent) | |
{ | |
base.MouseDown (theEvent); | |
((MyView)this.Animator).DrawLineWidth = NSNumber.FromNInt (DrawLineWidth.NIntValue + 10); | |
} | |
public override void DrawRect (CGRect dirtyRect) | |
{ | |
base.DrawRect (dirtyRect); | |
path.LineWidth = DrawLineWidth.NFloatValue; | |
color.SetStroke (); | |
path.Stroke (); | |
} | |
static CABasicAnimation drawnLineWidthBasicAnimation; | |
[Export ("defaultAnimationForKey:")] | |
static new NSObject DefaultAnimationFor (NSString key) | |
{ | |
if (key == "DrawnLineWidth") | |
{ | |
if (drawnLineWidthBasicAnimation == null) | |
{ | |
drawnLineWidthBasicAnimation = new CABasicAnimation (); | |
//drawnLineWidthBasicAnimation.Duration = 2.0f; | |
} | |
return drawnLineWidthBasicAnimation; | |
} | |
else | |
return NSView.DefaultAnimationFor (key); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment