2010-10-19 37 views
1

所以,我的文檔已經閱讀,使用的塊像CABasicAnimation問題

beginAnimation 
commitAnimation 

從OS4.0氣餒。

所以我試圖讓我的代碼通過使用CABasicAnimation工作。我想要實現的是,圖像的框架從其縮略圖大小(在我的視圖中的某處)調整到全寬度位置,例如(0,120,320,240) - 在我的iPhone上。

我有什麼至今:

[CATransaction begin]; 
[CATransaction setValue:[NSNumber numberWithFloat:1.0] forKey:kCATransactionAnimationDuration]; 

CABasicAnimation *scalingAnimation; 
scalingAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
scalingAnimation.duration=1.0/2; 
scalingAnimation.autoreverses=YES; 
scalingAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
scalingAnimation.fromValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]; 
scalingAnimation.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(4, 4, 1)]; 

[b.layer addAnimation:scalingAnimation forKey:@"scaling"]; 

[CATransaction commit]; 

我NEXTSTEP將首先嚐試將圖像移動到中心位置,然後擴展到正確的大小。但是,我懷疑我是以正確的方式做。任何人都可以評論我的代碼/方法....有更好的方法嗎?

回答

5

我很確定你現在已經解決了這個問題,但無論如何。

您不應該需要[CATransaction begin];和[CATransaction commit]; 我發現做這種事最簡單的方法是使用CAAnimationGroup並逐個添加動畫。

一個例子是

CABasicAnimation *scaleX = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"]; 
//this is not used, as the group provides the duration 
scaleX.duration = duration; 
scaleX.autoreverses = NO; 

scaleX.toValue = [NSNumber numberWithFloat:1.0]; 
scaleX.fromValue = [NSNumber numberWithFloat:scaleFrom]; 
scaleX.fillMode = kCAFillModeForwards; 
scaleX.removedOnCompletion = NO; 

CABasicAnimation *scaleY = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"]; 
//this is not used, as the group provides the duration 
scaleY.duration = duration; 
scaleY.autoreverses = NO; 

scaleY.toValue = [NSNumber numberWithFloat:1.0]; 
scaleY.fromValue = [NSNumber numberWithFloat:scaleFrom]; 
scaleY.fillMode = kCAFillModeForwards; 
scaleY.removedOnCompletion = NO; 

//add in the translation animations 

NSMutableArray* animationsArray = [NSMutableArray arrayWithObjects:scaleX, 
                    scaleY, 
//and any other animations you want in the group 
             nil]; 

CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; 
animationGroup.duration = 1.0/2; 
animationGroup.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn]; 
animationGroup.animations = animationsArray; 
animationGroup.delegate = self; 
animationGroup.removedOnCompletion = YES; 
animationGroup.fillMode = kCAFillModeForwards; 
[animationGroup setValue:@"imageTransform" forKey:@"AnimationName"]; 
[b.layer addAnimation:animationGroup forKey:@"imageTransform"]; 

有幾個陷阱,但。動畫純粹是可視化的,所以在運行動畫之前,請設置最終的視圖框架。

您會注意到縮放結束於1,這是爲了確保您不會結束縮放的圖像層。相反,我們開始縮放並恢復正常。

翻譯應該以同樣的方式完成。

希望這有助於

+0

感謝這個理查德。請注意,如果您複製並通過此代碼,請更改此行:CABasicAnimation * scaleY = [CABasicAnimation animationWithKeyPath:@「transform.scale.x」];說「transform.scale.y」 – DoctorG 2011-07-09 15:11:15

+0

@醫生:修正了他。 ;) – 2011-07-19 15:45:46

0

廣東話您使用

[UIView animateWithDuration:2.0 
         animations:^{ 
          //animations 
         } 
         completion:^(BOOL finished){ 
          // completion methods 
         }]; 
1
//in Event Method Copy Below code to place the image to the center 

    CABasicAnimation* positionAnimation; 
    positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
    positionAnimation.fromValue = [NSValue valueWithCGPoint:imageView.layer.position]; 
    positionAnimation.toValue = [NSValue valueWithCGPoint:centerPointofView]; 
    positionAnimation.duration = 2.0; 
    positionAnimation.fillMode = kCAFillModeForwards; 
    positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 
    positionAnimation.removedOnCompletion = NO; 
    positionAnimation.delegate = self; 
    [imageView.layer addAnimation:positionAnimation forKey:@"positionAnimation"]; 

// For Scale After image is in center copy below code 

    - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{ 
    CAAnimation *animation = [imageView.layer animationForKey:@"positionAnimation"]; 
    if (animation == theAnimation) 
    { 
     CABasicAnimation* scaleAnimation; 
     scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
     scaleAnimation.fromValue = [NSNumber numberWithFloat:1]; 
     scaleAnimation.toValue = [NSNumber numberWithFloat:4.0]; 
     scaleAnimation.duration = 2.2; 
     scaleAnimation.fillMode = kCAFillModeForwards; 
     scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 
     scaleAnimation.removedOnCompletion = NO; 
     scaleAnimation.delegate = self; 
     [imageView.layer addAnimation:scaleAnimation forKey:@"scaleAnimation"]; 
    } 
    else 
    { 
     //if you want changes to image should remain forever 
     //place your code for scale & transform here 
     .................... 
     //now simple remove animation from layer 
     [imageView.layer removeAllAnimations]; 
    } 
}