在同一动画中使用两个不同的UIViewAnimationCurve

时间:2012-07-12 09:38:21

标签: ios uiviewanimation uiviewanimation-curve

我希望能够使用一个UIViewAnimationCurve进行旋转,使用另一个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];

3 个答案:

答案 0 :(得分:4)

尝试创建两个不同的动画。 使用UIView的方法,您无法为动画的不同属性设置不同的动画曲线。 或者您可以使用CAAnimations获得乐趣并创建一个CAAnimationGroup,您可以在其中设置两个CABasicAnimation

答案 1 :(得分: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,则存在一些问题。

首先,动画组确定整个组的动画曲线。我不认为你可以为每个子动画指定不同的曲线(虽然我承认我没有尝试过。)

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

答案 2 :(得分: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];
}