繁体   English   中英

iOS旋转动画在CAAnimationGroup中无法顺利进行

[英]iOS rotation animation does not happen smoothly in CAAnimationGroup

我有3个动画在一组中运行:位置,比例和旋转

但是我的旋转动画无法顺利进行。 它在开始时突然发生,而其他动画则顺利发生。

这是我的代码:

//CABasicAnimation *scaleAnimation
//CABasicAnimation *moveAnimation
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
rotationAnimation.autoreverses = NO;  

rotationAnimation.toValue = [NSNumber numberWithFloat: 0];//-(M_PI/3)];
//below line shows the end state, if removed, the layer will assume its initial position after animation, something which I don't want.
[layer setTransform:CATransform3DMakeRotation(0, 0, 1, 0)];    

CAAnimationGroup *theGroup = [CAAnimationGroup animation];
//Code for adding all 3 animations to the group

编辑:

rotationAnimation.toValue = [NSNumber numberWithFloat:-(M_PI / 3)];

从动画声明的位置删除了最终的变换值,并将其添加到委托中。 (请注意,在动画组的情况下,单个动画委托不会被命中,只有组委托才能起作用)。

在代表下:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{   
    NSString * animName = [theAnimation valueForKey:@"animationName"];
    CALayer* layer = [theAnimation valueForKey:@"animationLayer"];

    if ([animName isEqualToString:@"MoveZoomOut"])
    {

       if ([layer.name isEqualToString:@"Layer1"])
          [layer setTransform:CATransform3DMakeRotation(-M_PI/3, 0, 1, 0)];
       else if ([layer.name isEqualToString:@"Layer2"])
          [layer setTransform:CATransform3DMakeRotation(M_PI/3, 0, 1, 0)];
       else
          [layer setTransform:CATransform3DMakeRotation(0, 0, 1, 0)];
    }

    if ([layer respondsToSelector:@selector(setContentsScale:)])
    {
        layer.contentsScale = [UIScreen mainScreen].scale;
    }
}

我有3个动画在一组中运行:位置,比例和旋转

我怀疑由于您同时对缩放和旋转进行了动画处理(这是通过transform属性完成的),因此可能会发生冲突。

通过将两个变换串联成一个,尝试在单个动画中设置缩放和旋转的动画:

CATransform3D t = CATransform3DMakeRotation(0, 0, 1, 0)];
[layer setTransform:CATransform3DScale(t, sx, sy, sz)]; 

编辑:

我知道发生的事情很不寻常,因此,我将向您提供一些提示,以帮助您数次获得帮助。 我了解,您需要设置3层动画。

  1. 将每层动画包装在[CATransaction begin]/[CATransaction commit]

如果那没有帮助,

  1. 在创建动画本身之后,不要立即设置要设置动画的属性的最终值; 而是在动画委托animationDidStop:finished:方法中进行设置。

换句话说,给定您的代码:

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
rotationAnimation.autoreverses = NO;  

rotationAnimation.toValue = [NSNumber numberWithFloat: 0];//-(M_PI/3)];
//below line shows the end state, if removed, the layer will assume its initial position after animation, something which I don't want.
[layer setTransform:CATransform3DMakeRotation(0, 0, 1, 0)];    

CAAnimationGroup *theGroup = [CAAnimationGroup animation];

不要做[layer setTransform:CATransform3DMakeRotation(0, 0, 1, 0)]; 那里。 相反,请执行以下操作:

- (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag {
   [layer setTransform:CATransform3DMakeRotation(0, 0, 1, 0)];
}

由于您将要进行多个动画(并且大概对所有动画都使用相同的委托),因此您将需要一种方法来告诉animationDidStop:方法所指的是哪个动画。 为此,您可以这样做,例如:

//-- se the delegate
rotationAnimation.delegate = self;
[rotationAnimation setValue:layer forKey:@"animationLayer"];

并在委托方法中:

- (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag {

   [CATransaction begin];
   [CATransaction setDisableActions:YES];

   CALayer* layer = [anim valueForKey:@"animationLayer"];
   layer.transform = ...;

   [CATransaction commit];

}

我知道这似乎有些人为。 是的。 但这是我可以解决与您所报告的问题类似的问题的唯一方法。

希望这可以帮助。

编辑:

对于使用animationDidStop:时的故障,请尝试将animationDidStop:内容包装在:

[CATransaction begin];
[CATransaction setDisableActions:YES];
...
[CATransaction commit];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM