我推荐的方法是将UIImage包装在UIImageView中,并使用CAKeyframeAnimation沿着通过三个点的路径为UIImageView的图层设置动画:
UIImage *image = [UIImage imageNamed:@"image.png"];
imageView = [[UIImageView alloc] initWithImage:image];
[mainView addSubview:imageView];
// Remember to remove the image view and release it when done with it
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 1.0f;
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
CGMutablePathRef pointPath = CGPathCreateMutable();
CGPathMoveToPoint(pointPath, NULL, viewOrigin.x, viewOrigin.y);
CGPathAddLineToPoint(pointPath, NULL, point1.x, point1.y);
CGPathAddLineToPoint(pointPath, NULL, point2.x, point2.y);
CGPathAddLineToPoint(pointPath, NULL, point3.x, point3.y);
pathAnimation.path = pointPath;
CGPathRelease(pointPath);
[imageView.layer addAnimation:pathAnimation forKey:@"pathAnimation"];
Run Code Online (Sandbox Code Playgroud)
请注意,默认情况下,图层的位置位于图层的中心.如果您想将图层相对于另一个参考点移动,可以将图层的anchorPoint属性设置为类似于(0.0,0.0)的左上角(在iPhone上)或(0.0,1.0),因为它的下部剩下.
此外,当它这样做不会改变的UIImageView的框架,所以如果你指的是框架以后,你可能需要既考虑到这一点,或添加一个委托方法回调动画的结尾设置达到适当的价值.
您还可以通过使用CGPathAddCurveToPoint()替换对CGPathAddLineToPoint()的调用,使图像沿曲线而不是直线移动.
编辑(2009年5月14日):我添加了缺少的pathAnimation.path = pointPath行,并将对curvePath的错误引用更改为pointPath.
| 归档时间: |
|
| 查看次数: |
9641 次 |
| 最近记录: |