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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using MonoMac.Foundation;
using MonoMac.AppKit;
using MonoMac.CoreAnimation;
using MonoMac.CoreGraphics;
namespace CustomAnimationTiming
{
public partial class CustomAnimationTiming : MonoMac.AppKit.NSView {
NSImageView mover;
CABasicAnimation moveAnimation;
public CustomAnimationTiming (IntPtr handle) : base(handle) {}
[Export("initWithCoder:")]
public CustomAnimationTiming (NSCoder coder) : base(coder) {}
[Export("initWithFrame:")]
public CustomAnimationTiming (RectangleF frame) : base(frame)
{
SetupMover ();
}
public override bool AcceptsFirstResponder ()
{
return true;
}
public override void KeyDown (NSEvent theEvent)
{
Move();
}
void SetupMover()
{
RectangleF moverFrame = Bounds.Inset(Bounds.Width / 4.0f,
Bounds.Height / 4.0f);
PointF origin = moverFrame.Location;
origin.X = 0.0f;
moverFrame.Location = origin;
mover = new NSImageView(moverFrame);
mover.ImageScaling = NSImageScale.AxesIndependently;
mover.Image = NSImage.ImageNamed("photo.jpg");
AddSubview(mover);
}
CABasicAnimation MoveItAnimation()
{
if (moveAnimation == null)
moveAnimation = new CABasicAnimation() {
Duration = 2,
TimingFunction = new CAMediaTimingFunction (0.5f, 1, 0.5f, 0)
};
return moveAnimation;
}
void Move()
{
var animations = NSDictionary.FromObjectAndKey (MoveItAnimation (), (NSString)"frameOrigin");
mover.Animations = animations;
PointF origin = mover.Frame.Location;
origin.X += mover.Frame.Width;
((NSView)mover.Animator).SetFrameOrigin(origin);
}
}
}
|