首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CABasicAnimation问题

CABasicAnimation问题
EN

Stack Overflow用户
提问于 2010-10-19 20:48:14
回答 3查看 9.1K关注 0票数 1

所以,我在文档中读到,使用像这样的块

代码语言:javascript
复制
beginAnimation
commitAnimation

从os4.0开始就不鼓励了。

因此,我尝试使用CABasicAnimation来让我的代码正常工作。我想实现的是,在我的iPhone上,图像的边框可以从缩略图大小调整到全宽位置,例如(0,120,320,240)。

到目前为止,我所拥有的:

代码语言:javascript
复制
[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];

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

EN

回答 3

Stack Overflow用户

发布于 2011-05-11 19:22:01

我很确定你现在已经解决了这个问题,但不管怎样。

你不应该需要CATransaction begin;和CATransaction commit;我发现做这种事情的最简单的方法是使用CAAnimationGroup和,然后一个接一个地添加动画。

一个例子就是

代码语言:javascript
复制
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结束,这是为了确保你不会以缩放的图像层结束。相反,我们开始缩放它并将其恢复到正常状态。

翻译应该以同样的方式进行。

希望这能有所帮助

票数 5
EN

Stack Overflow用户

发布于 2012-08-29 13:05:09

代码语言:javascript
复制
//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];
    }
}
票数 1
EN

Stack Overflow用户

发布于 2011-05-11 19:36:38

你不能用

代码语言:javascript
复制
[UIView animateWithDuration:2.0
                        animations:^{ 
                            //animations
                        } 
                        completion:^(BOOL finished){
                           // completion methods
                        }];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3968457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档