如何使用Monotouch同时在两个图层上应用动画

本文关键字:两个 图层 动画 应用 Monotouch 何使用 | 更新日期: 2023-09-27 18:07:58

我正在尝试使用Monotouch在屏幕上移动两个图像。我一直在研究cattransaction和Animation组,但是MonoTouch的例子很少,而且这两个对象似乎只适用于一个图层。

有人能指出在正确的方向上使用什么样的对象动画两个图像在同一时间?

感谢
        MovePos(img);
        MovePos(img2);
        public void MovePos (UIView view)
        {
            var theAnimation = CABasicAnimation.FromKeyPath ("position");
            theAnimation.Duration = 2;
            theAnimation.From = NSValue.FromPointF (new PointF (74, 74));
            theAnimation.To = NSValue.FromPointF (new PointF (566, 406));
            theAnimation.TimingFunction = new CAMediaTimingFunction (0.25f ,0.1f ,0.25f ,1.0f);
            view.Layer.AddAnimation (theAnimation, "animateLabel"); 
        }

如何使用Monotouch同时在两个图层上应用动画

您可以查看TweetStation的"swipe" cell支持的源代码,它可以一次执行各种动画:

  • 使单元格向右移动
  • 淡出图标
  • 它使图标增长,然后缩小到他们的大小
  • 跳出先前存在的菜单

所有这些都在单个事务中完成。源代码在:

https://github.com/migueldeicaza/TweetStation/blob/06e2ae5484103a85660201592bd7f06fb1b69395/TweetStation/UI/SwipeSupport.cs

动画实际上分布在不同的地方。SwipeMenuView在其构造函数上配置自己的动画(将图标淡入背景,增加/缩小图标),并将动画添加到CAAnimationGroup中,并通过使用layer将生成的动画添加到图层中。AddAnimation(集团):

这个方法的代码片段如下:

    var alpha = (CAKeyFrameAnimation) CAKeyFrameAnimation.FromKeyPath ("opacity");
    alpha.Values = new NSNumber [] {
        NSNumber.FromFloat (0),
        NSNumber.FromFloat (0.1f),
        NSNumber.FromFloat (1),
    };
    alpha.KeyTimes = new NSNumber [] {
        NSNumber.FromFloat (0),
        NSNumber.FromFloat (1f/(layers.Length-i)),
        NSNumber.FromFloat (1),
    };
    var size = (CAKeyFrameAnimation) CAKeyFrameAnimation.FromKeyPath ("transform.scale");
    size.Values = new NSNumber [] {
        NSNumber.FromFloat (0.7f),
        NSNumber.FromFloat (1.3f),
        NSNumber.FromFloat (1),
    };
    var group = CAAnimationGroup.CreateAnimation ();
    group.Animations = new CAAnimation [] { alpha, size /*, pos */ };
    group.Duration = delay; 
    layer.AddAnimation (group, "showup");                   

动画的另一部分是在ShowMenu中配置的,它将单元格从屏幕中滑动出来,如果已经显示了一个菜单,它会将单元格动画反射回它的新位置:

ShowMenu通过分组由UIView分隔的事务块中的更改来触发动画。BeginAnimations()和UIView。CommitAnimations:

        UIView.BeginAnimations ("Foo");
        UIView.SetAnimationDuration (hideDelay);
        UIView.SetAnimationCurve (UIViewAnimationCurve.EaseInOut);          
        var animation = MakeBounceAnimation (Math.Abs (offset), "position.x");
        foreach (var view in menuCell.ContentView.Subviews){
            if (view == currentMenuView)
                continue;
            view.SetNeedsDisplay ();
            AnimateBack (view, animation);
        }
        AnimateBack (menuCell.SelectedBackgroundView, animation);
        UIView.CommitAnimations ();

菜单动画被创建为具有弹跳效果:

CAAnimation MakeBounceAnimation (float offset, string key)
{
    var animation = (CAKeyFrameAnimation) CAKeyFrameAnimation.FromKeyPath (key);
    animation.Duration = hideDelay;
    float left = offset/2;
    animation.Values = new NSNumber [] {
        NSNumber.FromFloat (offset+left),
        NSNumber.FromFloat (left-30),
        NSNumber.FromFloat (left+10),
        NSNumber.FromFloat (left-10),
        NSNumber.FromFloat (left),
    };
    return animation;
}

AnimateBack方法只是附加动画:

void AnimateBack (UIView view, CAAnimation animation)
{
    var b = view.Bounds;
    view.Layer.Position = new PointF (b.Width/2, b.Height/2);
    view.Layer.AddAnimation (animation, "position");
}

我使用三种不同的方法来动画一个对象。根据您的需要,这个应该在您的控制范围内工作:

Animate (animationDuration, 0, UIViewAnimationOptions.CurveEaseOut, delegate {
    //your changes here
    Position = new PointF (566, 406);
    }, null);

如果没有帮助,您可以使用以下命令:

BeginAnimations("MovingStuff");
SetAnimationDuration(2f);
//your changes here, you can modify multiple objects at once and then commit it all together.
Position = new PointF (566, 406);
CommitAnimations();