Combining UIView perspective conversion with other CATranform3Ds

I am writing an application in such a way that sprites (subclasses UIImageView) can be rotated, resized and expanded around the screen using gestures. I would also like to be able to apply 3D perspective transformations to sprites.

I have a rotate / resize / pan function that works correctly, as well as perspective conversion. However, they seem to be working incorrectly. If I rotate an unmodified sprite, try to skew it, the sprite "resets" its rotation, and then applies perspective. However, the opposite works; if I skew first, I can apply any two-dimensional transformation after it is not reset.

Here is the code I'm using: (rotate, resize, and pan are done with UIGestureRecognizers, while skew uses UISlider).

Rotate:

- (void)didRotateSprite:(UIRotationGestureRecognizer *)rotate
{
    CGFloat angle = rotate.rotation;

    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DRotate(spriteView.layer.transform, angle, 0, 0, 1);

    spriteView.layer.transform = transform;

    rotate.rotation = 0.0;
}

Change of size:

- (void)didPinchSprite:(UIPinchGestureRecognizer *)pinch
{
    CGFloat scale = pinch.scale;

    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DScale(spriteView.layer.transform, scale, scale, 1);

    view.layer.transform = transform;

    pinch.scale = 1.0;
}

Perspective:

- (IBAction)perspectiveChanged:(UISlider *)slider
{
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = 1.0 / -100;
    transform = CATransform3DRotate(transform, (1 - (slider.value * 2)) * M_PI_2, 1, 0, 0);

    spriteView.layer.transform = transform;
}

Thank!

+4
source share
1 answer

Found an answer with lots of debugging and the help of this question. The trick was to transform the viewing perspective using:

spriteView.superview.layer.sublayerTransform = transform;

This recursively applies the transform to the viewview view and any subspecies it contains. For more information about this, documentation and Apple's “Layer Style Properties” .

+1

Source: https://habr.com/ru/post/1545736/


All Articles