I have a custom "card" view which has a corresponding XIB that lays out the subviews. I'm using constraints to layout the subviews.
I add the card view to another view and then attempt to animate it using CGAffineTransform rotationAngle
.
When I trigger the animation, the subviews of the card view shift suddenly depending on the rotationAngle. Meanwhile, the rest of the animation works as expected; it rotates at the angle I specified.
My questions are: why are the subviews changing position when the transform begins and how do I prevent that from happening?
Here's a sample of the code I'm using. view.layoutIfNeeded()
and view.autoresizesSubviews
do not seem to make a difference, FYI.
let rotation = arc4random_uniform(20)
let destinationCenterY = self.frame.height + view.frame.height
let destinationCenterX = arc4random_uniform(UInt32(self.frame.width))
let startTransform = CGAffineTransform(rotationAngle: CGFloat(rotation))
UIView.animate(withDuration: 0.4, animations: {
view.transform = startTransform
view.center.y = destinationCenterY
view.center.x = CGFloat(destinationCenterX)
view.layoutIfNeeded()
view.autoresizesSubviews = true
}, completion: { (complete) in
view.removeFromSuperview()
view.transform = endTransform
})
I also have a strange issue with subviews resizing when using CGAffineTransform scale
. Any help would be much appreciated.
EDIT: Adding images of what is happening. Before animation, views are aligned properly: After animation starts, the subviews move to the right:
To prevent my subviews from moving around on me when a transform started, I had to iterate through the subviews and set view.translatesAutoresizingMaskIntoConstraints = true
, as follows:
for subview in view.subviews {
subview.translatesAutoresizingMaskIntoConstraints = true
}
Thanks, @DevB2F for pointing me in the right direction!