【问题标题】:iPhone UIImageView with animation images needing to be moved along path带有动画图像的 iPhone UIImageView 需要沿路径移动
【发布时间】:2012-09-29 06:23:18
【问题描述】:
我有一个 UIImageView ,其中包含一组图像,它遍历以形成动画。
[imageView setAnimationImages:arrayOfImages];
我现在需要在动画制作时沿路径移动此图像视图。我浏览了this example的源代码。它使用 UIBezierPath 创建路径并使用 CALayer 表示沿路径移动的对象。但是我如何为动画 UIImageView 做到这一点?
非常感谢您的建议。
【问题讨论】:
标签:
iphone
animation
uiimageview
【解决方案1】:
[编辑] 请注意,我还必须将 UIImageView 作为子视图添加到当前视图。
终于用我之前给出的例子弄明白了。这是我的步骤:
-
创建路径。 (注意下面代码中的 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)];
....
-
创建 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];
-
设置 UIImageView 的图层位置并将图层添加到视图的图层:
[test.layer setPosition:P(160,25)];
[self.view.layer addSublayer:test.layer];
-
创建 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"];