0

I have a keyframe animation that worked great in one View Controller. I then copied that code to another similar VC. The difference is that this time the view being animated to move across the screen is one layer deeper, nested in an additional UIView. This seems to be causing a coordinates problem.

I get this destination x point like this:

let last_Pos = destinationImg.superview?.convertPoint(destinationImg.frame.origin, toView: nil)

Then in the keyframe, I set the x like this:

self.animatedView.frame.origin.x = last_Pos!.x

I did a print of "last_Pos" in both the original ViewController and the new. It's getting the same x value of 465, so that's not the problem. I'm pretty sure my problem is that in the second VC the 465 is being applied to the extra layer/UIView that it's nested in. I researched this but haven't found much written in Swift but it seems like Objective-C has a way of doing this.

My question is, how do I get x values on the same layer for these two views so I can animate the one to the other?

Community
  • 1
  • 1
Dave G
  • 12,042
  • 7
  • 57
  • 83

1 Answers1

0

As explained above, I was trying to get "absolute" x & y positions to animate two views in different hierarchies. If you don't know, when you check frame.origin.x or frame.origin.y you will get contextual x & y positions that referenced their parent views. I couldn't find an answer that worked and no one responded on this thread.

So... my answer was more like a patch-work fix that appears to work well for most situations:

I simply added another view in interface builder.

  • I named it "measuring stick"
  • And made it invisible
  • And made sure that it sat under all my button so that it wouldn't block button taps, etc.

For auto layout constraints, I connected one end to the view my animation should originate from and I connected the opposite end to the view I want my animate to end at. The magic is that it changes width and height with orientation changes, iPhone, iPad layouts, etc.

Then for my animation all my code does is check the "measuring sticks" width and height and uses those values for the change in x & y.

Hope this helps.

Dave G
  • 12,042
  • 7
  • 57
  • 83