Search code examples
iosswiftios-autolayoutcgaffinetransform

CGAffineTransform scale moves center of view on iOS10 (and doesn't on iOS11)


I have an app which supports both iOS10 and iOS11. In the app I have a uiview pinned to a finger via pan gesture. Pan gesture moves view center only along y axis (x stays the same always). I also have CGAffineTransform tied to the gesture, the more up user moves my view the larger it becomes. "Panned" view contains another view with some content.

Now on iOS11 I have this behavoiur (which I consider correct):

             > - center of content view stays on a vertical line
^  <--[      |      ]-->
|  <----[    |    ]---->
|  <------[  |  ]------>
|  <--------[ ]-------->

But on iOS10 I get this behavoiur:

                > - center of content view moves to the right along with scale growth 
^  <----[      /      ]>
|  <-----[    /    ]--->
|  <------[  /  ]------>
|  <--------[ ]-------->

My code to add content view to panned view:

// self is PannedView 
self.addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false
// add constraints:
// self.width = contentView.width
// self.height = contentView.height
// self.centerX = contentView.centerX
// self.centerY = contentView.centerY

Handle pan gesture:

... // handle some unrelated stuff
panView.center.y = self.panStartPoint.y + panDeltaY // where panDeltaY is basically a translation of a UIPanGestureRecognizer
... // some scale calculation logic, calc scale addition depending on pan touch location
let scale = 1 + movePercent * 0.1 // where movePercent is between 0 and 1
panView.transform = CGAffineTransform.identity.scaledBy(x: scale, y: scale)

As you can see I also cap scale to be between 1 and 1.1.

It seems like either constraints in conjunction with scaling work differently on iOS10 and iOS11 or scaling is "double" propagated to subviews somehow.

Any ideas?


Solution

  • Don't apply a transform to a view that is positioned by constraints. The results are undefined (which is exactly what you are experiencing: it "means" one thing on one system and another thing another).