LoginSignup
2
2

More than 5 years have passed since last update.

CoreAnimation: TODO: 止め方を調べる

Posted at
  • Windowをゆらす事はできるが止め方がよくわからない

WindowDidLoad() でゆらす:


    public partial class MainWindowController : MonoMac.AppKit.NSWindowController
    {
        //....

        public override void WindowDidLoad ()
        {
            base.WindowDidLoad ();

            Shake();
        }

アニメーションを作ってセットして、 Windowを移動すると揺れる。


        void Shake()
        {
            var key = new NSString (@"frameOrigin");
            var animation = CreateShakeAnimation (this.Window.Frame);
            var animations =  NSDictionary.FromObjectAndKey (animation, key);

            Window.Animations = animations;

            ((NSWindow)this.Window.Animator).SetFrameOrigin(
                Window.Frame.Location); 

        }

ゆらすアニメーションの作成


        CAKeyFrameAnimation CreateShakeAnimation(
            RectangleF frame, 
            int shakes = 100,
            float swing = 0.02f,
            float duration = 10.0f
        )
        {
            var shakePath = new CGPath();

            shakePath.MoveToPoint(frame.GetMinX(), frame.GetMinY());

            for (int index = 0; index < shakes; index++)
            {
                shakePath.CGPathAddLineToPoint(
                    frame.GetMinX() - frame.Size.Width * swing, frame.GetMinY());

                shakePath.CGPathAddLineToPoint(
                    frame.GetMinX() + frame.Size.Width * swing, frame.GetMinY());
            }

            shakePath.CloseSubpath();

            return new CAKeyFrameAnimation () {
                Path = shakePath,
                Duration = duration
            };
        }

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2