monotouch Get Image to move around the screen
Simple requirement but proving tricky.
T开发者_StackOverflow中文版he sample code below I threw together as an example, my end goal is to produce a routine that makes an image bounce on a given x co-ordinate. However at the moment I'm just stuck on getting the thing to move for the second time. The animation below only happens once i.e I cannot get it to move up and down regardless of the From and To parameters. It only moves once when loaded, i.e.
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");
}
That is because you are adding animations to the layer that animate the same property, while the previous animation has not yet completed.
Try adding the next animation when the previous animation completes by hooking to the CABasicAnimation.AnimationStopped
event. Do not forget to set the From
value to the previous To
value so that the next animation continues where the last one completed.
Also, set the RemovedOnCompletion
to true
so that you don't stack completed animations that you do not need in the Layer
.
精彩评论