ios - CAAnimation层取消了 View 的先前变换

标签 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"];

    }];

正如您在完成后所看到的,我正在向 View 添加另一个摇动动画:

[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 启动时, View “跳”回其原始位置。我怎样才能防止这种情况发生?为什么会发生这种情况?

谢谢

沙尼

最佳答案

新动画应考虑当前变换(平移):

    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是将要动画的图层。

关于ios - CAAnimation层取消了 View 的先前变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9874119/

相关文章:

iOS >> 动画 block >> 更改 block 执行的 Moving ImageView 方向 'In the Middle'

ios - Swift 3动画问题

ios - 有没有办法在 ios 的 uiwebview 中跟踪单个资源的加载

ios - 如何使 UINavigationController 从左推?

For 循环中的 IOS 动画。调整了每个延迟但仍然有问题

ios - 无法让 CALayer 在 UIView 中显示

iPhone:同步两个不同图层上的动画

ios - 通过移动和网络语音/语音解决方案进行情绪检测

android - 在 EditText 中输入 "ae"自动变成 æ

ios - UICollectionView 的动画框架和布局更改