使用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.
我正在尝试使用CGAffineTransform并找到了这段代码,但是我没有弄清楚要放在各个部分中的内容.
I am trying to use CGAffineTransform and have found this code but I am not figuring out what to put in the various parts.
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.
我假设图像视图将是我要更改的图像,但是它变成了magicMagic.和imageRightTop和imageRightBottom.
I assume image view would be my image that I want to change however what goes into somethingMagic. and imageRightTop and imageRightBottom.
我该怎么定义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.
谢谢
推荐答案
让我们假设您有一个名为imageView
的变量,其中包含对UIImageView
的引用.
我写了一些样本来演示如何实现此行为.该代码的作用是创建一个新的CGAffineTransform
矩阵.该矩阵具有与恒等变换矩阵相同的值,但有一个例外:位置[2,1]处的值.该值由CGAffineTransformMake
函数的c
参数控制,并控制沿x轴的剪切.您可以通过设置shearValue
来更改剪切量.
Let's assume you have a variable named imageView
holding a reference to your UIImageView
.
I wrote a little sample to demonstrate how you could get this behavior. What this code does is creating a new CGAffineTransform
matrix. This matrix has the same values as the identity transform matrix with one exception: the value at location [2,1]. This value is controlled by the c
-parameter of the CGAffineTransformMake
-function and controls the shearing along the x-axis. You can change the amount of shearing by setting shearValue
.
代码:
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];
雨燕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
这是shearTransform
-矩阵的样子:
[1 0 0]
[0.3 1 0]
[0 0 1]
这篇关于使用CGAffineTransform倾斜UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!