[Swift]iOS开发之锚点anchorPoint

众所周知,ios坐标系统是这样的⬇️

重点介绍锚点anchorPoint。我们来看看官方文档的介绍:

看到这几个坐标了么,这就是锚点!取值范围是0-1。

The default value of this property is (0.5, 0.5), which represents the center of the layer’s bounds rectangle.

默认值是(0.5,0.5),在layer的中心。⬇️它的作用。

applying a rotation transform to a layer with the default anchor point causes the layer to rotate around its center. Changing the anchor point to a different location would cause the layer to rotate around that new point.

下面我们讲的变化就要用到这个东西~

先来认识下iOS和OSX的坐标系统

这个position是相对superLayer的坐标,定义了layer的位置坐标。写个demo验证一下吧

var rectlayer:CALayer = CALayer()
var positionPoint:CALayer = CALayer()

 我先在sb里拉了一个UIView,把位置调到bounds坐标(0,0)处,大小40x40。

rectlayer.frame = CGRectMake(40, 40, 40, 40)
        rectlayer.backgroundColor = UIColor.redColor().CGColor
        self.view.layer.addSublayer(rectlayer)
let pPoint:CGPoint = rectlayer.position
        positionPoint.frame = CGRectMake(pPoint.x, pPoint.y, 5, 5)
        positionPoint.backgroundColor = UIColor.blackColor().CGColor
        self.view.layer.addSublayer(positionPoint)
print("\(rectlayer.position)")
     print("\(rectlayer.anchorPoint)")

 运行起来看下

print出来的东西:

从运行结果来看,position就是红色块的中心。锚地默认值确实是(0.5,0.5)。修改下锚点,运行结果⬆️

rectlayer.anchorPoint = CGPointMake(0, 0)

对比两个结果,position的值不变,但是红色方块的位置显然发生了改变。

不难看出,锚点定义了position相对于红色块layer的位置!通俗来说,锚点定义的位置就是position在红色块中的位置!验证一下吧~

不难看出,锚点定义的位置是,以frame定义的rect的左上角为坐标原点,xy轴方向同iOS系统坐标方向,rect的长宽分别为单位长度,定义的。

再通过官方介绍⬇️求证下,完全正确!

锚点是layer旋转,缩放等动画效果的动画中心,下一篇是关于layer 3d动画的解析。

 

转载于:https://www.cnblogs.com/ybw123321/p/5394408.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值