Scale with CGAffineTransform and set the anchor

旧街凉风 提交于 2019-11-28 17:05:10

问题


If I understand correctly scaling a UIView with CGAffineTransform anchors the transformation to its center.

In particular:

self.frame = CGRectMake(0,0,100,100);
self.transform = CGAffineTransformMakeScale(2, 2);
NSLog(@"%f;%f;%f;%f", self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height); 

Prints:

-50;-50;200;200

How do you create a CGAffineTransform scale that uses a specific anchor point (say 0;0)?


回答1:


(a)

Scale and then translate?

Something like :

CGAffineTransform t = CGAffineTransformMakeScale(2, 2);
t = CGAffineTransformTranslate(t, width/2, height/2);
self.transform = t;

(b)

Set the anchor point (which is probably what you want really)

[self layer].anchorPoint = CGPointMake(0.0f, 0.0f);
self.transform = CGAffineTransformMakeScale(2, 2);

(c)

Set the center again to make sure it's in the same place?

CGPoint center = self.center;
self.transform = CGAffineTransformMakeScale(2, 2);
self.center = center;



回答2:


Firstly #import <QuartzCore/QuartzCore.h> and then set the anchor points of your view:

   [[self layer] setAnchorPoint:CGPointMake(0, 0)];


来源:https://stackoverflow.com/questions/8757613/scale-with-cgaffinetransform-and-set-the-anchor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!