为什么图像过渡会影响我的动画?

时间:2014-07-21 18:51:07

标签: ios objective-c animation

我在图像视图上运行常量动画:

[UIView animateWithDuration:dur
                      delay:curAnim.delay
                    options:(params)
                 animations:^{
                     self.view.layer.opacity = curAnim.opacity;
                     self.view.center = curAnim.center;
                     CGAffineTransform tr = CGAffineTransformMakeRotation(curAnim.rotation);
                     tr = CGAffineTransformScale(tr, curAnim.scale, curAnim.scale);
                     self.view.transform = tr;

                 }
                 completion:^(BOOL finished){

                 }];

然后,当常量动画正在运行时,我想转换到另一个图像:

-(void)transitionToImage:(UIImage*)image duration:(CGFloat)duration
{
    UIImageView* iv = (UIImageView*)self.view;

    iv.image = image;

    CATransition *transition = [CATransition animation];
    transition.duration = duration;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;

    [iv.layer addAnimation:transition forKey:@"transition"];
}

然而,当我转换时,现在有2个视图,一个移动而一个不移动,而不是在我的图像视图上发生转换。

我尝试了各种转换方法,但都会产生同样的问题。

我怎么能这样做才能转换并继续当前正在运行的动画?

1 个答案:

答案 0 :(得分:3)

对两个动画使用核心动画

CABasicAnimation *posAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
posAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 250)];
posAnimation.fromValue = [NSValue valueWithCGPoint:self.imageView.layer.position];
posAnimation.duration = 3;
posAnimation.fillMode = kCAFillModeForwards;
posAnimation.removedOnCompletion = NO;
[self.imageView.layer addAnimation:posAnimation forKey:nil];

CABasicAnimation *imgAnim = [CABasicAnimation animationWithKeyPath:@"contents"];
imgAnim.fromValue = (id)[UIImage imageNamed:@"icon1"].CGImage;
imgAnim.toValue = (id)[UIImage imageNamed:@"icon2"].CGImage;
imgAnim.duration = 3;
imgAnim.fillMode = kCAFillModeForwards;
imgAnim.removedOnCompletion = NO;
[self.imageView.layer addAnimation:imgAnim forKey:nil];

CoreAnimation(CATransition)并没有真正改变属性。 [UIView animateWithDuration:...]的确如此。所以CATransition并不关心当前的位置。它会在动画开始的位置抓取您的图像,并仅在“presentationLayer”(而不是图层本身)上执行转换,该转换始终位于相同的位置。

<强>更新

CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnim.fromValue = @1;
scaleAnim.toValue = @2;
scaleAnim.duration = 3;
scaleAnim.fillMode = kCAFillModeForwards;
scaleAnim.removedOnCompletion = NO;
[self.imageView.layer addAnimation:scaleAnim forKey:nil];


CABasicAnimation *rotAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotAnim.fromValue = @0;
rotAnim.toValue = @(M_PI);
rotAnim.duration = 3;
rotAnim.fillMode = kCAFillModeForwards;
rotAnim.removedOnCompletion = NO;
[self.imageView.layer addAnimation:rotAnim forKey:nil];
相关问题