【问题标题】:CAKeyFrameAnimation of transform property on CALayer not working as expectedCALayer 上变换属性的 CAKeyFrameAnimation 未按预期工作
【发布时间】:2012-12-19 05:57:06
【问题描述】:

我已经为此工作了一段时间,并彻底搜索了解决方案,但无济于事。这就是我想要做的。

我有一个 UIScrollView,用户可以在其上缩放和平移 5 秒。我有一个单独的 CALayer,它没有分层在 UIScrollView 之上。我想缩放和翻译这个 CALayer 的内容以反映 UIScrollView 上发生的缩放和平移。我想通过关键帧动画CAKeyFrameAnimation 来实现这一点。当我将其放入代码时,缩放按预期发生,但内容的位置偏移不正确。

这是我在代码中的做法。假设 UIScrollView 委托将缩放比例和内容偏移量传递给以下方法:

// Remember all zoom params for late recreation via a CAKeyFrameAnimation
- (void) didZoomOrScroll:(float)zoomScale
           contentOffset:(CGPoint)scrollViewContentOffset {

    CATransform3D scale = CATransform3DMakeScale(zoomScale, zoomScale, 1);
    CATransform3D translate = CATransform3DMakeTranslation(-scrollViewContentOffset.x, 
                                                           -scrollViewContentOffset.y, 0);
    CATransform3D concat = CATransform3DConcat(scale, translate);

    // _zoomScrollTransforms and _zoomScrollTimes below are of type NSMutableArray
    [_zoomScrollTransforms addObject:[NSValue valueWithCATransform3D:concat]];

    // secondsElapsed below keeps track of time
    [_zoomScrollTimes addObject:[NSNumber numberWithFloat:secondsElapsed]];
}

// Construct layer animation
- (void) constructLayerWithAnimation:(CALayer *)layer {

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 5.0;
    animation.values = _zoomScrollTransforms;

    // Adjust key frame times to contain fractional durations
    NSMutableArray *keyTimes = [[NSMutableArray alloc] init];
    for( int i = 0; i < [_zoomScrollTimes count]; i++ ) {
          NSNumber *number = (NSNumber *)[_zoomScrollTimes objectAtIndex:i];
          NSNumber *fractionalDuration = [NSNumber numberWithFloat:[number          floatValue]/animation.duration];
         [keyTimes addObject:fractionalDuration];
    }
    animation.keyTimes = keyTimes;

    animation.removedOnCompletion = YES;
    animation.beginTime = 0;
    animation.calculationMode = kCAAnimationDiscrete;
    animation.fillMode = kCAFillModeForwards;
    [layer addAnimation:animation forKey:nil];
}

当上面的图层动画时,内容被正确缩放但位置不正确。内容似乎是x 和y 的变化超出了我的预期,因此不能完全追溯用户在 UIScrollView 上所做的缩放/平移。

任何想法我可能做错了什么?

回答

好的,我知道我做错了什么。它与 CALayer 的锚点有关。 CALayers 上的变换总是应用于锚点。对于 CALayers,锚点默认为 (0.5, 0.5)。所以缩放和平移是沿着中心点进行的。另一方面,UIScrollViews 从视图的左上角给出偏移量。基本上您可以将 UIScrollView 的锚点视为(0.0,0.0),以考虑 CGPoint 偏移值。

所以解决办法就是将CALayer的锚点设置为(0.0, 0.0)。然后一切都按预期进行。

还有其他资源以更好的方式呈现此信息。请参阅 Stackoverflow 上的另一个问题 similar。另请参阅 Apple 文档中的 this article,其中详细讨论了几何中的位置、锚点和一般层。

【问题讨论】:

    标签: ios uiscrollview calayer cakeyframeanimation translate-animation


    【解决方案1】:

    您正在将平移矩阵与缩放矩阵连接起来。位移偏移的最终值将是 Offset(X, Y) = (scaleX * Tx, scaleY * Ty)。

    如果您想使用 (Tx, Ty) 偏移量移动 UIView,则连接平移和缩放矩阵如下:

    CATransform3D scale = CATransform3DMakeScale(zoomScale, zoomScale, 1);
    CATransform3D translate = CATransform3DMakeTranslation(-scrollViewContentOffset.x, 
                                                           -scrollViewContentOffset.y, 0);
    CATransform3D concat = CATransform3DConcat(translate, scale);
    

    试试这个,让我知道它是否有效。

    【讨论】:

    • 谢谢@mak。我在提交帖子之前尝试过。我又试了一次,它比CATransform3DConcat(scale,translate) 更远。不幸的是,它不起作用。
    猜你喜欢
    • 1970-01-01
    • 2020-04-26
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 2011-08-28
    相关资源
    最近更新 更多