2011-02-18 75 views
1

简单的要求,但证明棘手。monotouch获取图像在屏幕上移动

下面的代码示例代码放在一起作为示例,我的最终目标是生成一个例程,使图像在给定x坐标上反弹。然而目前我只是第二次让事情继续前进而停滞不前。 下面的动画只发生一次,即无论From和To参数如何都无法上下移动。它仅在加载时移动一次,即

 private void LoadGetStarted() { 
     UIImageView imageView = new UIImageView(); 
     UIImage getStartedImage = new UIImage("images/DownArrow.gif"); 
     float leftPos = 80f; 
     RectangleF f2 = new RectangleF(leftPos, 
              130f, 
              200f, 
              181f); 
     imageView.Image = getStartedImage; 
     imageView.Frame = f2; 
     this.Add(imageView); 

     Animate(imageView,"y",-100f,-200f); 
     Animate(imageView,"y",-100f,200f); 
     Animate(imageView,"y",10,100f); 

    } 

    private void Animate(UIImageView imageView,string type,float top, float bottom) { 
     var theAnimation = CABasicAnimation.FromKeyPath("transform.translation."+type); 
     theAnimation.Duration = 3; 
     theAnimation.Speed = 2; 
     theAnimation.FillMode = CAFillMode.Both; 
     theAnimation.From = NSNumber.FromFloat(top); 
     theAnimation.To = NSNumber.FromFloat(bottom); 
     theAnimation.RemovedOnCompletion = false; 
     imageView.Layer.AddAnimation(theAnimation,"imageView"); 
    } 

回答

1

这是因为您正在为动画相同属性的图层添加动画,而以前的动画尚未完成。 尝试通过挂钩CABasicAnimation.AnimationStopped事件来完成上一个动画时添加下一个动画。不要忘记将From值设置为之前的To值,以便在最后一个完成的地方继续下一个动画。 另外,请将RemovedOnCompletion设置为true,以便您不会堆叠Layer中不需要的完整动画。