调用AnchorPoint方法

时间:2016-09-04 03:35:21

标签: objective-c

我发现这段代码调整了对象的图层大小,以便在更改其锚点时保持对象的位置相同。我非常确定代码本身是有效的,因为许多用户评论它是如何完美地工作的。但作为一个新手,我不知道如何称呼这种方法。我想将Ball对象的锚点从(2.0,0.5)更改为(0.5,0.5)。

-(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;
}

我尝试了很多

的变体
[self setAnchorPoint:{0.5, 0.5}anchorPoint forView:(Ball*)];

无济于事

1 个答案:

答案 0 :(得分:0)

您的密码:

[self setAnchorPoint:{0.5, 0.5}anchorPoint forView:(Ball*)];

我的代码: 您应该使用目标C代码来调用方法。

[self setAnchorPoint:CGPointMake(0.5,0.5) forView:Ball];

供您参考访问链接: http://www.tutorialspoint.com/objective_c/objective_c_functions.htmhttp://rypress.com/tutorials/objective-c/methods