5

我希望能够使用一个 UIViewAnimationCurve 进行旋转,另一个用于位置变化。这可能吗?

例如(在伪代码中);

// Begin animations

// Set rotation animation curve to EaseInOut

// Set position animation curve to Linear

// Make some changes to position

// Make some change to the angle of rotation

// Commit the animations

编辑:(下面建议的 CAAnimationGroup 方法) - 创建了 2 个单独的 CABasicAnimations 和一个 CAAnimationGroup 但是动画没有开始。有任何想法吗?

CABasicAnimation *postionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
postionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
postionAnimation.toValue = [NSValue valueWithCGPoint:[self transformPointToWorldSpaceFromViewSpace:self.spriteView.position]];
postionAnimation.delegate = self;

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.z"];
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
rotationAnimation.toValue = [NSNumber numberWithFloat:self.spriteView.angle -= M_PI_2];
rotationAnimation.delegate = self;

CAAnimationGroup *animationsGroup = [CAAnimationGroup animation];
animationsGroup.duration = self.clockSpeed;
animationsGroup.animations = [NSArray arrayWithObjects:postionAnimation, rotationAnimation, nil];

// Perform the animation
[self.spriteView.layer addAnimation:animationsGroup forKey:nil];
4

3 回答 3

4

尝试创建两个不同的动画。使用 UIView 的方法,您不能为动画的不同属性设置不同的动画曲线。或者你可以玩 CAAnimations 并创建一个 CAAnimationGroup 来设置你的两个 CABasicAnimation

于 2012-07-12T09:41:59.660 回答
3

最好的办法是多次调用 animateWithDuration:delay:options:animations:completion:。在每次调用中使用不同的动画曲线,它们将并行运行。或者,使用不同的延迟值,您可以让它们按顺序运行。

代码可能如下所示:

[UIView animateWithDuration: .5 
  delay: 0
  options: UIViewAnimationOptionCurveEaseInOut 
  animations: ^{
    view1.center = CGPointMake(x, y);
  }
  completion:  ^{
    //code that runs when this animation finishes
  }
];

[UIView animateWithDuration: .5 
  delay: .5
  options: UIViewAnimationOptionCurveLinear
  animations: ^{
    view2.center = CGPointMake(x2, y2);
  }
  completion:  ^{
    //code that runs when this animation finishes
  }
];

如果你使用 CAAnimationGroups,会有几个问题。

首先,动画组决定了整个组的动画曲线。我认为您不能为每个子动画指定不同的曲线(尽管我承认我没有尝试过。)

其次,如果您将动画添加到组中,则不会调用各个动画的代理。只有动画组的委托方法被调用。

于 2012-07-14T01:41:09.187 回答
0

好的,终于解决了。感谢 iSofTom 的指导(对您的答案投了赞成票)。我设置了单个动画的代表,但没有设置组。

CABasicAnimation *postionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
postionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
postionAnimation.fromValue = [NSValue valueWithCGPoint:self.spriteView.center];
postionAnimation.toValue = [NSValue valueWithCGPoint:[self transformPointToViewSpaceFromWorldSpace:self.spriteView.position]];

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
rotationAnimation.fromValue = [NSNumber numberWithFloat:self.spriteView.angle];
rotationAnimation.toValue = [NSNumber numberWithFloat:self.spriteView.rotation];

CAAnimationGroup *animationsGroup = [CAAnimationGroup animation];
animationsGroup.duration = self.clockSpeed;
animationsGroup.animations = [NSArray arrayWithObjects:postionAnimation, rotationAnimation, nil];
animationsGroup.delegate = self;
// Perform the animation
[self.spriteView.layer addAnimation:animationsGroup forKey:nil];

- (void)animationDidStart:(CAAnimation *)theAnimation {
radiansToDegrees(self.spriteView.rotation));
    self.spriteView.angle = self.spriteView.rotation;

NSStringFromCGPoint(self.spriteView.position));
    self.spriteView.center = [self transformPointToViewSpaceFromWorldSpace:self.spriteView.position];
}
于 2012-07-12T21:12:44.707 回答