CABasicAnimation问题

发布于 2024-09-28 06:52:08 字数 1024 浏览 8 评论 0原文

所以,我在文档中读到,

beginAnimation
commitAnimation

os4.0 不鼓励使用类似的块。

因此,我尝试使用 CABasicAnimation 使我的代码正常工作。我想要实现的目标是,在我的 iPhone 上,图像的框架从视图中某处的缩略图大小调整为全宽位置,例如(0、120、320、240)。

到目前为止我所拥有的:

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

我的下一步是首先尝试将图像移动到居中位置,然后将其缩放到正确的大小。然而,我怀疑我的做法是否正确。任何人都可以评论我的代码/方法......有更好的方法吗?

So, I have read in the docs, that use of blocks like

beginAnimation
commitAnimation

is discouraged from os4.0.

So I have tried to get my code to work by using CABasicAnimation. I want to achieve, that an image's frame is resized from its thumbnail size, somewhere within my view, to a full width position e.g. (0, 120, 320, 240) - on my iPhone.

What I have so far:

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

My nextstep would be to first try to move the image to a centered position then scale it to the correct size. However, I doubt I'm doin it the right way. Can anyone comment on my code/approach.... is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦归所梦 2024-10-05 06:52:08

我很确定你现在已经解决了这个问题,但无论如何。

您不应该需要 [CATransaction begin];和 [CATransaction 提交];
我发现执行此类操作的最简单方法是使用 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 结束,这是为了确保您最终不会得到缩放的图像层。相反,我们开始对其进行缩放并使其恢复正常。

翻译应该以同样的方式完成。

希望这有帮助

Am pretty sure you have solved this by now, but in any case.

You shouldn't need the [CATransaction begin]; and [CATransaction commit];
The simplest way that I have found to do this kind of thing is to use CAAnimationGroup and and add the animations one by one.

An example would be

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

There are a few gotchas though. Animations are purely visual, so before running the animations, set your eventual view frame.

You will notice that the scales end at 1, this is to ensure that you dont end up with a scaled image layer. Instead we start it scaled and bring it back to normal.

Translations should be done in the same way.

Hope this helps

以歌曲疗慰 2024-10-05 06:52:08
//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];
    }
}
//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];
    }
}
彩虹直至黑白 2024-10-05 06:52:08

你不能用吗

[UIView animateWithDuration:2.0
                        animations:^{ 
                            //animations
                        } 
                        completion:^(BOOL finished){
                           // completion methods
                        }];

Cant you use

[UIView animateWithDuration:2.0
                        animations:^{ 
                            //animations
                        } 
                        completion:^(BOOL finished){
                           // completion methods
                        }];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文