更改我的CALayer的anchorPoint将移动视图 [英] Changing my CALayer's anchorPoint moves the view

查看:214
本文介绍了更改我的CALayer的anchorPoint将移动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变 anchorPoint ,但保持视图在同一个地方。
我尝试过 NSLog -ing self.layer.position self .center ,并且它们都保持不变,无论锚点的更改如何。

 <$ c $ 



c> self.layer.anchorPoint = CGPointMake(0.5,0.5);
NSLog(@center point:%f%f,self.layer.position.x,self.layer.position.y);
self.layer.anchorPoint = CGPointMake(1,1);
NSLog(@center point:%f%f,self.layer.position.x,self.layer.position.y);

输出为:

  2009-12-27 20:43:24.161类型[11289:207]中心点:272.500000 242.500000 
2009-12-27 20:43:24.162类型[11289:207] :272.500000 242.500000


解决方案

图层几何和Core Animation Programming Guide中的Transforms 部分解释了CALayer的position和anchorPoint属性之间的关系。基本上,图层的位置是根据图层的anchorPoint的位置来指定的。默认情况下,图层的anchorPoint是(0.5,0.5),它位于图层的中心。当您设置图层的位置时,您将在其上层坐标系中设置图层中心的位置。



因为位置是相对于anchorPoint ,改变anchorPoint同时保持相同的位置移动该层。为了防止此移动,您需要调整图层的位置以考虑新的anchorPoint。我这样做的一种方式是抓取图层的边界,将边界的宽度和高度乘以旧的和新的anchorPoint的归一化值,取两个anchorPoints的差异,并应用该差异到图层的位置。



您甚至可以通过在UIView的CGAffineTransform中使用 CGPointApplyAffineTransform() / p>

I want to alter the anchorPoint, but keep the view in the same place. I've tried NSLog-ing self.layer.position and self.center and they both stay the same regardless of changes to the anchorPoint. Yet my view moves!

Any tips on how to do this?

self.layer.anchorPoint = CGPointMake(0.5, 0.5);
NSLog(@"center point: %f %f", self.layer.position.x, self.layer.position.y);
self.layer.anchorPoint = CGPointMake(1, 1);
NSLog(@"center point: %f %f", self.layer.position.x, self.layer.position.y);

The output is:

2009-12-27 20:43:24.161 Type[11289:207] center point: 272.500000 242.500000
2009-12-27 20:43:24.162 Type[11289:207] center point: 272.500000 242.500000

解决方案

The Layer Geometry and Transforms section of the Core Animation Programming Guide explains the relationship between a CALayer's position and anchorPoint properties. Basically, the position of a layer is specified in terms of the location of the layer's anchorPoint. By default, a layer's anchorPoint is (0.5, 0.5), which lies at the center of the layer. When you set the position of the layer, you are then setting the location of the center of the layer in its superlayer's coordinate system.

Because the position is relative to the anchorPoint of the layer, changing that anchorPoint while maintaining the same position moves the layer. In order to prevent this movement, you would need to adjust the layer's position to account for the new anchorPoint. One way I've done this is to grab the layer's bounds, multiply the bounds' width and height by the old and new anchorPoint's normalized values, take the difference of the two anchorPoints, and apply that difference to the position of the layer.

You might even be able to account for rotation this way by using CGPointApplyAffineTransform() with your UIView's CGAffineTransform.

这篇关于更改我的CALayer的anchorPoint将移动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆