2011-02-18 39 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中不需要的完整動畫。