1

だから、私はドキュメントを読みました、そのようなブロックの使用

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];

私の次のステップは、最初に画像を中央の位置に移動してから、正しいサイズにスケーリングすることです. しかし、私はそれを正しい方法で行っているとは思えません。誰でも私のコード/アプローチについてコメントできますか....もっと良い方法はありますか?

4

3 に答える 3

5

これで解決したと確信していますが、いずれにせよ。

[CATransaction begin] は必要ありません。[CATransaction コミット]; この種のことを行うために私が見つけた最も簡単な方法は、CAAnimationGroup を使用して、アニメーションを 1 つずつ追加することです。

例は

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 で終わることに気付くでしょう。これは、縮尺されたイメージ レイヤーになってしまわないようにするためです。代わりに、スケーリングを開始し、通常に戻します。

翻訳も同様に行う必要があります。

お役に立てれば

于 2011-05-11T11:22:00.993 に答える
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];
    }
}
于 2012-08-29T05:05:08.863 に答える
0

使えませんか

[UIView animateWithDuration:2.0
                        animations:^{ 
                            //animations
                        } 
                        completion:^(BOOL finished){
                           // completion methods
                        }];
于 2011-05-11T11:36:38.300 に答える