简体   繁体   English

使用 CGAffineTransform 倾斜 UIImageView

[英]skewing a UIImageView using CGAffineTransform

I am trying to skew a rectangle so the two vertical sides are slanted but parallel and the top and bottom are horizontal.我试图倾斜一个矩形,使两个垂直边倾斜但平行,顶部和底部水平。

I am trying to use CGAffineTransform and have found this code but I am not figuring out what to put in the various parts.我正在尝试使用 CGAffineTransform 并找到了此代码,但我不知道在各个部分中放入什么。

imageView.layer.somethingMagic.imageRightTop = (CGPoint){ 230, 30 };
                imageView.layer.somethingMagic.imageRightBottom = (CGPoint){ 300, 150 };

#define CGAffineTransformDistort(t, x, y) (CGAffineTransformConcat(t, CGAffineTransformMake(1, y, x, 1, 0, 0)))
#define CGAffineTransformMakeDistort(x, y) (CGAffineTransformDistort(CGAffineTransformIdentity, x, y))

although this is said to be easy I don't know what to put in the different places.虽然据说这很容易,但我不知道在不同的地方放什么。

I assume image view would be my image that I want to change however what goes into somethingMagic.我假设图像视图将是我想要更改的图像,但是什么会变成 somethingMagic。 and imageRightTop and imageRightBottom.和 imageRightTop 和 imageRightBottom。

Also how do I define t.还有我如何定义 t.

If there is a more thorough explanation I would appreciate it since in most cases I found only this as the explanation of what to do to skew a rectangle.如果有更彻底的解释,我将不胜感激,因为在大多数情况下,我发现这只是对如何扭曲矩形的解释。

Thanks谢谢

Let's assume you have a variable named imageView holding a reference to your UIImageView .假设您有一个名为imageView的变量,其中包含对UIImageView的引用。
I wrote a little sample to demonstrate how you could get this behavior.我写了一个小示例来演示如何获得这种行为。 What this code does is creating a new CGAffineTransform matrix.这段代码的作用是创建一个新的CGAffineTransform矩阵。 This matrix has the same values as the identity transform matrix with one exception: the value at location [2,1].该矩阵与单位变换矩阵具有相同的值,但有一个例外:位置 [2,1] 处的值。 This value is controlled by the c -parameter of the CGAffineTransformMake -function and controls the shearing along the x-axis.该值由CGAffineTransformMake函数的c参数控制,并控制沿 x 轴的剪切。 You can change the amount of shearing by setting shearValue .您可以通过设置shearValue来更改剪切量。

The code:代码:

Objective-C目标-C

CGFloat shearValue = 0.3f; // You can change this to anything you want
CGAffineTransform shearTransform = CGAffineTransformMake(1.f, 0.f, shearValue, 1.f, 0.f, 0.f);
[imageView setTransform:shearTransform];

Swift 5斯威夫特 5

let shearValue = CGFloat(0.3) // You can change this to anything you want
let shearTransform = CGAffineTransform(a: 1, b: 0, c: shearValue, d: 1, tx: 0, ty: 0)
imageView.transform = shearTransform

And here's what the shearTransform -matrix looks like:这是shearTransform样子:

[1     0     0]  
[0.3   1     0]  
[0     0     1]   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM