简体   繁体   中英

How do I stretch a CALayer while maintaining an integral origin?

I'm trying to stretch a UIView that I have by applying a CATransform3DMakeScale to it:

CATransform3DMakeScale(0.9, 1.0 - (0.1 * (path.row - midPath.row)), 1)

Problem with this is two fold: it leaves me with fractional frames/bounds, which I can't seem to round or floor, and it pulls my frame's origin down so that it's y origin point is no longer 0.

Is there a way that I can stretch my layer in one direction only? In other words, keep the x and y origin fixed and stretch either towards this origin or away from it.

Also, is there documentation anywhere for what the different mxx variables in a CATransform3D affect? I looked around and couldn't find any, and trial and error only got me so far.

If you want to maintain a fixed origin when stretching your CALayer, you can change its anchorPoint property like the following:

layer.anchorPoint = CGPointMake(0.0f, 0.0f);

The anchorPoint determines the point about which transforms are applied to your layer. It also is tied to your layer's position property, so if you adjust the anchorPoint you may need to reposition your layer afterwards.

By setting the anchor point, you should be able to set your layer at an integral origin coordinate and have it stretch with your transform. You won't maintain an integral width and height, however.

As far as the m12 , etc. elements of a CATransform3D, that struct is 4 x 4 matrix:

struct CATransform3D
{
CGFloat m11, m12, m13, m14;
CGFloat m21, m22, m23, m24;
CGFloat m31, m32, m33, m34;
CGFloat m41, m42, m43, m44;
};
typedef struct CATransform3D CATransform3D;

where each value represents one location in the matrix. This matrix is based on OpenGL's 3-D matrices, like the model view matrix. Core Animation provides helper functions to do most of the manipulation of this matrix that you'll need, but every now and then you'll set one of the matrix elements (like m34 for a perspective effect).

I've never been good at matrix math, but you can look up any of the writeups that people have put together for the math behind OpenGL model view matrices and apply that here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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