如何构建核心动画CATransform3D矩阵以动画同时转换和缩放

Sal*_*rtz 1 core-animation matrix calayer ios

我想同时缩放和翻译CALayer,从一个CGrect(一个小的,从一个按钮)到另一个(一个更大的,居中的,一个视图).基本上,想法是用户触摸按钮,然后从按钮中,CALayer显示并翻译和缩放以最终在屏幕上居中.然后CALayer(通过另一个按钮)缩小回按钮的位置和大小.

我通过CATransform3D矩阵制作动画.但CALayer实际上是UIView的支持层(因为我还需要Responder功能).虽然单独应用我的比例或翻译变换工作正常.两者的连接(平移,后跟缩放)会偏移图层的位置,以便在缩小时不与按钮对齐.

我的猜测是,这是因为CALayer锚点默认位于其中心.变换首先应用平移,移动"大"CALayer以与其框架左上角的按钮对齐.然后,当进行缩放时,由于CALayer锚点位于中心,所有方向都向下缩小.此时,我的图层是按钮的大小(我想要的),但是位置是偏移的(导致所有点向图层中心缩小).

说得通?

所以我想弄清楚是否不是连接翻译+规模,我需要:

翻译

将锚点更改为左上角.

规模.

或者,如果我能够提出一些因子或常数来合并到平移矩阵的值,那么它将转换为位置偏移,后续缩放将依次偏移,然后最终位置将是对.

有什么想法吗?

rob*_*off 7

你应该发布你的代码.当我们查看您的代码时,我们通常会更容易帮助您.

无论如何,这对我有用:

- (IBAction)showZoomView:(id)sender {
    [UIView animateWithDuration:.5 animations:^{
        self.zoomView.layer.transform = CATransform3DIdentity;
    }];
}

- (IBAction)hideZoomView:(id)sender {
    CGPoint buttonCenter = self.hideButton.center;
    CGPoint zoomViewCenter = self.zoomView.center;
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DTranslate(transform, buttonCenter.x - zoomViewCenter.x, buttonCenter.y - zoomViewCenter.y, 0);
    transform = CATransform3DScale(transform, .001, .001, 1);
    [UIView animateWithDuration:.5 animations:^{
        self.zoomView.layer.transform = transform;
    }];
}
Run Code Online (Sandbox Code Playgroud)

在我的测试用例中,self.hideButtonself.zoomView拥有相同的superview.