2012-07-12 52 views
5

我希望能够使用一个UIViewAnimationCurve进行旋转,而另一个用于更改位置。这可能吗?在同一个动画中使用两个不同的UIViewAnimationCurves

例如(在伪代码中);

// 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

最好的办法是使用多个调用animateWithDuration:延迟:选项:动画:完成:.在每个调用中使用不同的动画曲线,它们将并行运行。或者,使用不同的延迟值,可以使它们按顺序运行。

的代码可能是这样的:

[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,有几个问题。

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

其次,如果向动画组添加动画,则单个动画的委托不会获得调用。只有动画组的委托方法才会被调用。

+0

这是很好的建议,谢谢邓肯。明天会放弃它。戴夫 – 2012-07-14 18:40:32

+0

嗨邓肯,做到这一点,并工作的一种享受!我唯一的问题是,可以在块中调用我的方法[self transform.ToViewSpaceFromWorldSpace:self.spriteView.position]吗?它的工作原理和仪器没有显示任何泄漏,但我隐约记得有关在块内使用自己的东西。再次感谢戴夫。 – 2012-07-16 07:54:48

4

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

+0

感谢iSofTom,尝试过这种方法(请参阅编辑问题),但根据委托方法,我的动画并未开始。有任何想法吗? – 2012-07-12 11:04:30

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]; 
} 
相关问题