首页 > 开发 > iOS > 正文

CABasicAnimation中的锚点

2017-09-08 14:02:44  来源:网友分享

define PI 3.14159265

-(void)rotation{    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];    animation.fromValue = [NSNumber numberWithFloat:0];    animation.toValue   = [NSNumber numberWithFloat:-PI/4];    animation.duration  = 1.0f;    [view.layer addAnimation:animation forKey:nil];}

这段代码可以让这个view逆时针旋转45度 但是以为这个view的中心点为中心旋转的,如果我想以这个view的左下角的点为中心旋转,应该是添加什么样的代码呢?

解决方案

设置一下Anchor Points即可。

下图中,上半部分的anchorPoint为(0.5, 0.5),即anchorPoint为layer的中心。下半部分的anchorPoint为(0.0, 0.0),即anchorPoint为layer的左上角。同理我们可知,anchorPoint如果为(1.0, 1.0)的化就代表右下角。(注意iOS与OS X坐标系的不同)

下面这张图更清晰的解释了Anchor Points对layer的transformations变化的影响:

对于iOS,可以总结anchorPoint值对应layer的位置如下图:

对于你的问题,只需要设置view.layer的anchorPoint为(0.0, 1.0)即可以左下角为中心旋转。

参考连接:Core Animation Programming Guide