CALayer之anchorPoint分析

CALayer之anchorPoint分析anchorPoint:CALayer中心点,动画特效的中心点,取值区间[0.0,1.0],默认为(0.5,0.5);position:CALayer中心点坐标;frame.origin:由anchorPoint、position共同计算得出:frame.origin.x=position.x-anchorPoint*bounds.size.wi

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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;

CALayer之anchorPoint分析

修改anchorPoint:

CGPoint anchorPoint = CGPointMake(0.0, 0.5);

btnBlue.layer.anchorPoint = anchorPoint;

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

CALayer之anchorPoint分析

解决办法:

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就显示正常了。


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/184497.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月8日 下午5:46
下一篇 2022年10月8日 下午5:46


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号