您的位置:首页 > 其它

CALayer之anchorPoint分析

2014-04-29 19:05 148 查看
anchorPoint:CALayer中心点,动画特效的中心点,取值区间[0.0, 1.0],默认为(0.5, 0.5);

position:CALayer中心点坐标;

frame.origin:由anchorPoint、position共同计算得出:

frame.origin.x = position.x - anchorPoint * bounds.size.width;

frame.origin.y = position.y - anchorPoint * bounds.size.height;

frame.size.width = bounds.size.width;

frame.size.height = bounds.size.height;

有些动画效果需要我们修改anchorPoint,比如绕着自身某条边旋转等。

当我们改变了anchorPoint,计算出的frame.origin也会随之改变,因此最终显示的图像就会发生偏移,这时就需要我们对position或frame做适当的修改。

例如:在屏幕上显示一个蓝色button

UIButton *btnBlue = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];

btnBlue.backgroundColor = [UIColor blueColor];

CGRect oldFrame = btnBlue.layer.frame;



修改anchorPoint:

CGPoint anchorPoint = CGPointMake(0.0, 0.5);

btnBlue.layer.anchorPoint = anchorPoint;

修改之后,btnBlue显示位置向右偏移。



解决办法:

1.修改position

btnBlue.layer.position = CGPointMake(btnBlue.layer.position.x - btnBlue.layer.frame.size.width * (0.5 - anchorPoint.x),

                                                                      btnBlue.layer.position.y - btnBlue.layer.frame.size.height * (0.5 - anchorPoint.y);

2.直接修改frame

btnBlue.layer.frame = oldFrame.

这样,btnBlue就显示正常了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  anchorPoint 锚点