I am trying to scale a UIView during user interaction and I implemented the CGAffineTransform on the view as follows:
@implementation Tileview
{
CGAffineTransform _tempTransform;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_tempTransform = self.transform;
self.transform = CGAffineTransformScale(self.transform, 1.2, 1.2);
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.transform = _tempTransform;
}
This seemed to scale the view well but I noticed later that I started to have unpredictable results when moving views around on the screen, and doing some research I learned that this is possibly because I am using autolayout. The recommendation was to animate the layer rather than the view object.
So I attempted to refactor the above code to use CATranform3D
becauseit seemed that the CAAffineTransform
could not be applied to the layer. But a CATransform3D
could be. But I haven't been able to figure out how to make the layer scale using that Transform object.
I have attempted to apply a transform to the View's layer as follows:
self.layer.sublayerTransform = CATransform3DMakeScale(1.2, 1.2, 0.0f);
And that scales all the sublayers of my view
But I try to apply that to
self.layer = CATransform3DMakeScale(1.2, 1.2, 0.0f);
I get an error saying
Assignment to readonly property
I also looked at the CATransformLayer
class, but when try to assign that to self.layer I get an error saying:
Assignment to CATransformLayer from incompatible type CATransform3D
So my question is, is there a way to scale the UIView's layer without interfering with the Autolayout coordinants.
Why are you using sublayer transform? This should work:
self.layer.transform = CATransform3DScale(self.layer.transform, 1.2, 1.2, 1);
And also you can apply affine transforms to a layer if you really need to via CATransform3DMakeAffineTransform(<#CGAffineTransform m#>)
I've noticed that you are saving the initial transform to apply it after touches has ended. You can restore initial transform simply by
self.layer.transform = CATransform3DIdentity;