1

How do I check if a particular UIView's transform is either was set via CGAffineTransformIdentity or was moved physically?

Scenario:
I can position UIView via either changing its frame or via transformation. Once the UIView has been positioned either via transform or change of frame... I wish to revert it back to its original layout via a different button.

Observation:
1) If I had changed the UIView's frame, I can revert it back via original frame values.
2) If I had transformed the UIView, I can revert it back via the CGAffineTransformIdentity.

So...How would I know the difference, programmatically?
I'm assuming by testing the transform. How exactly?

P.S. This purely for edification.

var transform = CGAffineTransformIdentity

if origFrame != containerView.frame {
   // TODO: Need to determine if reset transform CGAffineTransformIdentity vs moving containerView.
   print("1b) Reset Layout")

} else {
   print("1a) Animate Transform.")
   transform = CGAffineTransformScale(transform, 0.25, 0.25)
   transform = CGAffineTransformTranslate(transform, -400, -600)
}

UIView.animateWithDuration(1.0, animations: {
    self.containerView.transform = transform
})
Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • Possible duplicate of [How to know the current scale of a UIView?](http://stackoverflow.com/questions/12040044/how-to-know-the-current-scale-of-a-uiview) – Chris Slowik Feb 19 '16 at 00:18
  • Doesn't work. I always get '1' as the scale factor if the view was transformed to the upper left (shrink & moved) or merely moved. – Frederick C. Lee Feb 19 '16 at 21:36
  • The information about how transform matrices work was correct and relevant to your question. https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGAffineTransform/index.html#//apple_ref/c/tdef/CGAffineTransform – Chris Slowik Feb 20 '16 at 01:45

1 Answers1

0

To test a CGAffineTransform on equality use the function CGAffineTransformEqualToTransform. In your case compare to CGAffineTransformIdentity. In ObjectiveC:

if (CGAffineTransformEqualToTransform(self.containerView.transform, CGAffineTransformIdentity))
    DoSomething;
C. Ecker
  • 165
  • 1
  • 8