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!