Skew UIImageView using CGAffineTransform

I'm trying to skew the rectangle, so the two vertical sides are tilted but parallel, and the top and bottom are horizontal.

I try to use CGAffineTransform and found this code, but I do not understand what to put in different 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 it is said to be easy, I don’t know what to put in different places.

I assume that the image image will be my image, which I want to change, but what happens in somethingMagic. and imageRightTop and imageRightBottom.

Also how to determine t.

If there is a more detailed explanation, I would be grateful, since in most cases I found only this as an explanation of what needs to be done to skew the rectangle.

thanks

+4
source share
1 answer

Suppose you have a variable called imageView that contains a link to your UIImageView .
I wrote a small sample to demonstrate how you can get this behavior. What this code does is create a new CGAffineTransform matrix. This matrix has the same values ​​as the identical transformation matrix, with one exception: the value at location [2,1]. This value is controlled by the parameter c the CGAffineTransformMake function and controls the shift along the x axis. You can change the amount of shift by setting shearValue .

Code:

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

And this is what the shearTransform matrix looks like:

 [1 0 0] [0.3 1 0] [0 0 1] 
+6
source

All Articles