UIView动画用弓,不是直线

时间:2013-03-06 17:12:34

标签: objective-c animation uiview uiviewanimation

我希望我的动画有一个弓而不是一条直线 这是我的代码:

UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-125, 100, 125, 125)];
    logoImageView.image = logoImage;
    logoImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:logoImageView];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
logoImageView.frame = CGRectMake(screenRect.size.width - 192, 190, logoImageView.frame.size.width, logoImageView.frame.size.height);
[UIView commitAnimations];

我也试过这个,但它不起作用(它只需要从最后一个中心移动)。

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
logoImageView.center = CGPointMake ...
logoImageView.center = CGPointMake ...
logoImageView.center = CGPointMake ...
  

logoImageView.frame = CGRectMake(screenRect.size.width - 192, 190, logoImageView.frame.size.width, logoImageView.frame.size.height);
[UIView commitAnimations];

我该怎么做?

提前谢谢。

1 个答案:

答案 0 :(得分:3)

正如我在评论中所提到的,您需要CAKeyframeAnimation来执行多个这样的值。简单的方法是只指定值并指定calculationMode如何插值。如果这不能提供您想要的结果,您可以指定任何CGPath您想要对视图进行动画制作。您尝试做的代码看起来像这样。

CAKeyframeAnimation *curvedAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
curvedAnimation.duration = 3.0;
curvedAnimation.calculationMode = kCAAnimationCubic;
curvedAnimation.values = @[[NSValue valueWithCGPoint:currentPoint],
                           [NSValue valueWithCGPoint:firstPoint],
                           [NSValue valueWithCGPoint:secondPoint],
                           [NSValue valueWithCGPoint:endPoint]];

curvedAnimation.fillMode = kCAFillModeBackwards; // Show first value before animation begins

logoImageView.layer.position = endPoint; // Change position to end value (Core Animation only performs the animation, it won't change the property you are animating)
[logoImageView.layer addAnimation:curvedAnimation
                           forKey:@"my bow animation"];

如果您是Core Animation的新手。您可以在QuartzCore.framework中找到它,您必须将其添加到项目中以及代码中的#import <QuartzCore/QuartzCore.h>