2012-07-12 90 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]; 
} 
相關問題