CALayer - CABasicAnimation不围绕center / anchorPoint进行缩放 [英] CALayer - CABasicAnimation not scaling around center/anchorPoint

查看:505
本文介绍了CALayer - CABasicAnimation不围绕center / anchorPoint进行缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码我一直试图让我的CALayer围绕它的中心进行缩放 - 但是它按左/上(0,0)进行缩放。我看到了这个问题: CALayer中的锚点并尝试设置anchorPoint - 但它是[ .5,.5]在我设置之前,设置没有任何区别。还尝试设置 contentsGravity

Using the code below I've been trying to get my CALayer to scale around its center - but it scales by left/top (0,0). I saw this question: Anchor Point in CALayer and have tried setting the anchorPoint - but it was [.5, .5] before I set it and setting makes no difference. Also tried setting contentsGravity.

设置是我有一个 CAShapeLayer 已添加到 self.view.layer 我在其中进行了一些绘制,然后添加了CABasicAnimation。动画运行正常 - 但它向上/左侧缩放 - 而不是视图的中心。

The set up is I have a CAShapeLayer added to self.view.layer I do some drawing in it and then add the CABasicAnimation. The animation runs fine - but it scales toward the top/left - not the center of the view.

几个小时一直在修补它 - 它必须是某种东西很简单,但我没有得到它。

Have been tinkering with this for a few hours - it must be something simple but I'm not getting it.

shapeLayer.anchorPoint = CGPointMake(.5,.5);
shapeLayer.contentsGravity = @"center";

printf("anchorPoint %f  %f\n", shapeLayer.anchorPoint.x, shapeLayer.anchorPoint.y);

CABasicAnimation *animation =
[CABasicAnimation animationWithKeyPath:@"transform.scale"];

animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.toValue =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];

[animation setDuration:1.0];
animation.fillMode=kCAFillModeForwards;
animation.removedOnCompletion=NO;
[shapeLayer addAnimation:animation forKey:@"zoom"];


推荐答案

什么是界限您的图层?

我敢打赌它的尺寸为零;尝试将其大小设置为与形状相同的大小。

I bet it has zero size; try setting its size to the same size as your shape.

CAShapeLayer 将形状绘制为覆盖类型,独立于内容(和 bounds contentsGravity 等等。)你在普通的 CALayer 中使用。这可能有点令人困惑。

CAShapeLayer draws the shape as kind of an overlay, independent of the contents (and bounds and contentsGravity and etc.) that you'd use in an ordinary CALayer. It can be a bit confusing.

这篇关于CALayer - CABasicAnimation不围绕center / anchorPoint进行缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆