iOS开发·如何理解anchorPoint和position的作

2021-01-27  本文已影响0人  富城
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(100, 100, 100, 100);
layer.backgroundColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:layer];
CGPoint point = layer.frame.origin;    // (CGPoint) point = (x = 100, y = 100)
CGPoint position = layer.position;   // (CGPoint) position = (x = 150, y = 150)
CGPoint anc = layer.anchorPoint; // 是比例  (CGPoint) anc = (x = 0.5, y = 0.5)
CGSize size = layer.bounds.size;  // (CGSize) size = (width = 100, height = 100)
layer.anchorPoint = CGPointMake(0.5, 0);   // 修改了锚点的值
CGPoint point1 = layer.frame.origin;   // (CGPoint) point1 = (x = 100, y = 150)
CGPoint position1 = layer.position;   // (CGPoint) position1 = (x = 150, y = 150)
CGPoint anc1 = layer.anchorPoint;   // 是比例  (CGPoint) anc1 = (x = 0.5, y = 0)
CGSize size1 = layer.bounds.size;   //  (CGSize) size1 = (width = 100, height = 100)

从上面的代码可以看出:frame.origin.x = position.x - anchorPoint.x * bounds.size.width;
frame 和 position 是父视图坐标参考,anchorPoint和bounds是视图本身坐标.

上一篇 下一篇

猜你喜欢

热点阅读