user278859
user278859

Reputation: 10519

I do not understand why/how CGAffineTransformTranslate works

I am creating a view and having it move on to the screen from left to right. I had a hard time getting it to work the way I wanted but when I did I was baffled at the way it worked.

I thought I would have to create the view off screen to the left then use CGAffineTransformTranslate to move it onto the screen. But instead I am creating it on the screen, but it still works great. I am a bit confused.

Here I am creating the view...

profileViewControllerForIPad = [[ProfileViewController alloc] initWithFrame:CGRectMake(0, 0, 512, 446)];

As you can see the origin is 0,0. So on screen, right?

Here is the thransform...

[UIView animateWithDuration:0.5 
         animations:^{
            self.view.transform = CGAffineTransformTranslate(CGAffineTransformIdentity,self.view.frame.size.width, 0.0);                             
         }
 ];

As you can see I am setting the tx value to the width of the CGAffineTransformTranslate which I thought was the value for the number of pixels you wanted it to move from it's current position, origin.x of 0, which I expected would move it to the middle of the screen somewhere. Instead, it slides right in with it's new origin.x right at 0 where I want it.

BTW CGAffineTransformIdentity works great sliding the view right back off the screen.

Can someone explain this to me. The docs seem to say something different.

Thanks,

John

Upvotes: 0

Views: 3588

Answers (2)

osxdirk
osxdirk

Reputation: 560

I think you can animate the frame property (- animateWithDuration:animations:) of your viewControllers -view instead of using a transform, which is much easier to use and understand.

Upvotes: 0

user278859
user278859

Reputation: 10519

I think I understand it now. I think it depends on when you execute CGAffineTransformTranslate.

If you are creating the object and call CGAffineTransformTranslate before you add it to it's superview, when you add it to it's superview it will display at a position that will allow it to move to it's defined position the amount and direction defined in the CGAffineTransformTranslate call.

If on the other hand the object you want to move is already being displayed, the object will be moved from it's current position to the new position.

In my case where I was creating a new UIView and wanted it to slide in from the left side of the screen, if I called CGAffineTransformTranslate at the same time that I created and added the view, I would have to define the view's frame at it's final on screen location.

Alternatively one could call CGAffineTransformTranslate in a timer method. In this case you would define the view's frame off screen, add it to the display and create the timer. The timer method would then move it on screen with CGAffineTransformTranslate.

Upvotes: 1

Related Questions