예제 #1
0
        async void OnViewModelPropertyChanged(string property)
        {
            switch (property)
            {
            case nameof(this.ViewModel.NextTitle):
                button.Title = this.ViewModel.NextTitle;
                break;

            case nameof(this.ViewModel.Current):
                await view.AnimateAsync(this.ViewModel.Current);

                break;
            }
        }
        public static async Task AnimateAsync(this NSView view, IAnimation animation)
        {
            // Tell the view to create a backing layer.
            view.WantsLayer = true;
            view.LayerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay;

            if (animation is TransformAnimation transformAnimation)
            {
                view.Layer.AnchorPoint = new CGPoint(0.5f, 0.5f);

                var source = new TaskCompletionSource <bool>();

                var curve = transformAnimation.Curve.ToNative();
                var from  = transformAnimation.From;
                var to    = transformAnimation.To;
                var w     = (float)view.Bounds.Width;
                var h     = (float)view.Bounds.Height;

                view.Layer.AddAnimation("opacity", curve, from.Opacity, to.Opacity, (anim) => anim.AnimationStopped += (sender, e) => source.SetResult(true))
                .AddAnimation("transform.scale.x", curve, from.ScaleX, to.ScaleX)
                .AddAnimation("transform.scale.y", curve, from.ScaleY, to.ScaleY)
                .AddAnimation("transform.translation.x", curve, from.TranslateX * w, to.TranslateX * w)
                .AddAnimation("transform.translation.y", curve, -1 * from.TranslateY * h, -1 * to.TranslateY * h)
                .AddAnimation("transform.rotation.z", curve, from.Rotation, to.Rotation);

                view.Layer.Opacity   = transformAnimation.To.Opacity;
                view.Layer.Transform = transformAnimation.To.ToNative(view.Bounds.Size);

                await source.Task;
            }
            else if (animation is SequenceAnimation sequenceAnimation)
            {
                foreach (var childAnimation in sequenceAnimation)
                {
                    await view.AnimateAsync(childAnimation);
                }
            }
        }