为了账号安全,请及时绑定邮箱和手机立即绑定

更改我的CALayer的anchorPoint会移动视图

更改我的CALayer的anchorPoint会移动视图

拉丁的传说 2019-09-18 19:32:25
我想改变anchorPoint,但保持视图在同一个地方。我试过NSLog-ing分词self.layer.position和self.center他们都保持不变,无论更改anchorPoint的。然而我的观点却在变化关于如何做到这一点的任何提示?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 Type[11289:207] center point: 272.500000 242.5000002009-12-27 20:43:24.162 Type[11289:207] center point: 272.500000 242.500000
查看完整描述

3 回答

?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

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


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


您甚至可以通过使用CGPointApplyAffineTransform()UIView的CGAffineTransform以这种方式考虑轮换。


查看完整回答
反对 回复 2019-09-18
?
holdtom

TA贡献1805条经验 获得超10个赞

我有同样的问题。Brad Larson的解决方案即使在视图旋转时也能很好地工作。这是他的解决方案翻译成代码。


-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view

{

    CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, 

                                   view.bounds.size.height * anchorPoint.y);

    CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, 

                                   view.bounds.size.height * view.layer.anchorPoint.y);


    newPoint = CGPointApplyAffineTransform(newPoint, view.transform);

    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);


    CGPoint position = view.layer.position;


    position.x -= oldPoint.x;

    position.x += newPoint.x;


    position.y -= oldPoint.y;

    position.y += newPoint.y;


    view.layer.position = position;

    view.layer.anchorPoint = anchorPoint;

}

而快速的等价物:


func setAnchorPoint(anchorPoint: CGPoint, forView view: UIView) {

    var newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y)

    var oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y)


    newPoint = CGPointApplyAffineTransform(newPoint, view.transform)

    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform)


    var position = view.layer.position

    position.x -= oldPoint.x

    position.x += newPoint.x


    position.y -= oldPoint.y

    position.y += newPoint.y


    view.layer.position = position

    view.layer.anchorPoint = anchorPoint

}

SWIFT 4.x


func setAnchorPoint(anchorPoint: CGPoint, forView view: UIView) {

    var newPoint = CGPoint(x: view.bounds.size.width * anchorPoint.x,

                           y: view.bounds.size.height * anchorPoint.y)



    var oldPoint = CGPoint(x: view.bounds.size.width * view.layer.anchorPoint.x,

                           y: view.bounds.size.height * view.layer.anchorPoint.y)


    newPoint = newPoint.applying(view.transform)

    oldPoint = oldPoint.applying(view.transform)


    var position = view.layer.position

    position.x -= oldPoint.x

    position.x += newPoint.x


    position.y -= oldPoint.y

    position.y += newPoint.y


    view.layer.position = position

    view.layer.anchorPoint = anchorPoint

}


查看完整回答
反对 回复 2019-09-18
  • 3 回答
  • 0 关注
  • 743 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信