ios 使用 CGAffineTransform 缩放并设置锚点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8757613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:11:05  来源:igfitidea点击:

Scale with CGAffineTransform and set the anchor

iphoneiosscalingcgaffinetransform

提问by hpique

If I understand correctly scaling a UIViewwith CGAffineTransformanchors the transformation to its center.

如果我理解正确缩放 a UIViewwithCGAffineTransform锚点转换到其中心。

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)?

您如何创建使用特定锚点(例如 0;0)的 CGAffineTransform 比例尺?

回答by deanWombourne

(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)

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

回答by Mudit Bajpai

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

首先#import <QuartzCore/QuartzCore.h>,然后设置视图的锚点:

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