Search code examples
iosswiftuiimageviewaffinetransform

Are there any UV Coordinates (or similar) for UIImageView?


I have a simple UIImageView in my view, but I can't seem to find any feature in Apple's documentation to change the UV Coordinates of this UIImageView, to convey my idea to you, this GIF file should preview how changing 4 vertices coordinates can change how the image gets viewed on the final UIImageView.

I tried to find a solution online too (other than documentation) and found none.

Enter image description here

I use Swift.


Solution

  • You can achieve that very animation using UIView.transform or CALayer.transform. You'll need basic geometry to convert UV coordinates to a CGAffineTransform or CATransform3D.

    I made an assumption that affine transform would suffice because in your animation the transform is affine (parallel lines stay parallel). In that case, 3 vertices are free -- the 4th one is constrained by the other 3.

    If you have 3 vertices, you can compute the affine transform matrix using: Affine transformation algorithm

    To achieve the infinite repeat, use UIImageResizingMode.Tile.