在我看来,最简单的方法是将动画代码放入一个方法中,并根据需要多次调用该方法。未经测试的代码,但它应该可以工作,或者至少给你一个想法。
// Repeat 10 times, move 20 right and the left and right etc.
FancyAnim(activeCell, activeCell.Bounds.Location, 10, 20);
private void FancyAnim(UITableViewCell activeCell, PointF originalLocation, int repeat, float offset)
{
var bounds = activeCell.Bounds;
var loc = originalLocation;
UIView.Animate(0.2,
delegate
{
// Called when animation starts.
loc.X = originalLocation.X + offset;
activeCell.Bounds = new RectangleF (loc, bounds.Size);
},
delegate
{
// Called when animation ends.
repeat--;
// Call the animation method again but invert the movement.
// If you don't do this too often, you should not run out of memory because of a stack overflow.
if(repeat >= 0)
{
FancyAnim(activeCell, originalLocation, repeat, -offset);
}
});
但是,您也可以使用路径动画。您将定义一条路径“向右 20 个单位,回到中心,向左 20 个单位,回到中心”,并根据需要重复该动画。这需要你处理 CAKeyFrameAnimation 并且会稍微多一些代码。这个网站可以让你快速入门:http ://www.bdunagan.com/2009/04/26/core-animation-on-the-iphone/