2012-10-09 149 views
0

我有一个UIImageView与它迭代通过形成一个动画的图像数组。iPhone UIImageView与动画图像需要沿着路径移动

[imageView setAnimationImages:arrayOfImages]; 

我现在需要的,而它是动画沿着路径移动这个图像视图。我浏览了this example的源代码。它使用UIBezierPath创建路径,并使用CALayer来表示沿路径移动的对象。但是,我会如何做这个动画UIImageView?

非常感谢您的任何建议。

回答

1

[编辑]请注意我还必须将UIImageView作为子视图添加到当前视图。

最后用我以前给的例子搞清楚了。这里是我的步骤:

  1. 创建路径。 (注意P = CGPointMake在下面的代码)

    UIBezierPath *trackPath = [UIBezierPath bezierPath]; 
    [trackPath moveToPoint:P(160, 25)]; 
    [trackPath addCurveToPoint:P(300, 120) 
         controlPoint1:P(320, 0) 
         controlPoint2:P(300, 80)]; 
    [trackPath addCurveToPoint:P(80, 380) 
         controlPoint1:P(300, 200) 
         controlPoint2:P(200, 480)]; 
    

    ....

  2. 创建的UIImageView,并给它的动画图像的阵列。

    UIImageView *test = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; 
    [test setAnimationImages:[NSArray arrayWithObjects:[UIImage imageNamed:@"bee-1"],  
    [UIImage imageNamed:@"bee-2"], nil]]; 
    [test startAnimating]; 
    
  3. 设置的UIImageView的层位置和层添加到视图中的层:

    [test.layer setPosition:P(160,25)]; 
    [self.view.layer addSublayer:test.layer]; 
    
  4. 创建CAKeyframeAnimation:

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
    anim.path = trackPath.CGPath; 
    anim.rotationMode = kCAAnimationRotateAuto; 
    anim.repeatCount = HUGE_VALF; 
    anim.duration = 8.0; 
    [test.layer addAnimation:anim forKey:@"race"];