CAAnimation层取消视图的previos转换

时间:2012-03-26 14:33:21

标签: ios uiviewanimation caanimation

我正在使用简单的转换动画到UIIMageView。

    [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut animations:^{
        CGAffineTransform translate = CGAffineTransformMakeTranslation(0, -[self percentFromHeight:20]);
        clueImage.transform = translate;
    } completion:^(BOOL finished) {
        [self.cluWordSelectionView setaWordsObjects:[NSArray arrayWithArray:[self.whiteCard clueWordObjectsForSoundAtIndex:self.index]]];

        [self addSubview:cluWordSelectionView];
        [clueImage.layer addAnimation:[Animations shakeAnimation] forKey:@"shake"];

    }];

正如您在完成后所看到的那样,我在视图中添加了另一个摇动动画:

[clueImage.layer addAnimation:[Animations shakeAnimation] forKey:@"shake"];

看起来像这样:

+(CAAnimation*)shakeAnimation {

CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];

CGFloat wobbleAngle = 0.06f;

NSValue* valLeft = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(wobbleAngle, 0.0f, 0.0f, 1.0f)];
NSValue* valRight = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-wobbleAngle, 0.0f, 0.0f, 1.0f)];
animation.values = [NSArray arrayWithObjects:valLeft, valRight, nil];
animation.beginTime = 3;
animation.autoreverses = YES;  
animation.duration = 0.125;
animation.repeatCount = HUGE_VALF;  

return animation;

}

问题在于,shakeAnimation启动视图时会“跳转”回原来的位置。我怎样才能防止这种情况发生?为什么会发生这种情况?

由于

SHANI

1 个答案:

答案 0 :(得分:1)

新动画应考虑当前的转换(翻译):

    CATransform3D leftTransform = CATransform3DMakeRotation(wobbleAngle, 0.0f, 0.0f, 1.0f);
    leftTransform = CATransform3DConcat(layer.transform, leftTransform);
    CATransform3D rightTransform = CATransform3DMakeRotation(-wobbleAngle, 0.0f, 0.0f, 1.0f);
    rightTransform = CATransform3DConcat(layer.transform, rightTransform);

    NSValue* valLeft = [NSValue valueWithCATransform3D:leftTransform];
    NSValue* valRight = [NSValue valueWithCATransform3D:rightTransform];

layer是要动画的图层。