0

我正在对 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开始视图“跳”回到它的原始位置。我该如何防止这种情况以及为什么会发生这种情况?

谢谢

沙尼

4

1 回答 1

1

新动画应该考虑到当前的变换(translation):

    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将被动画化的图层在哪里。

于 2012-03-26T14:55:46.653 回答