iOS how to start image off screen and animate it onto the screen

2.2k views Asked by At

I am trying to slide an image from off the screen onto the screen from the left and stopping at the center point in the view. I would like to keep it contrained at the y position if possible (IE image is set in story board).

I am reading this tutorial, but it is in swift and I cannot make the same assignments in objective-c.

http://www.raywenderlich.com/95910/uiview-animation-swift-tutorial

It says to set the view off the screen (in swift):

heading.center.x  -= view.bounds.width

Then animate (in swift):

UIView.animateWithDuration(0.5, animations: {
    self.heading.center.x += self.view.bounds.width
})

However I cannot manipulate center.x in objective c like they are doing in swift.

I have tried adjusting the initial position of the image in viewWillAppear by changing the bounds with no luck.

EDIT:

Here is what I am trying to get it positioned off the screen:

self.heading.center = CGPointMake(-200, self.heading.center.y);

No matter how negative I set the x position the view will still appears on the screen. Actually no matter what I set x position to the view does not move in the x direction at all. I have also tried to set the frame

2

There are 2 answers

4
Gamma On

The x coordinate of the view's center is not directly assignable in Objective-C. Instead, try setting the center point as whole. In your example, this could look something like this:

[UIView animateWithDuration:0.5 animations:^{
    CGFloat newCenterX = self.heading.center.x + self.view.bounds.size.width;
    self.heading.center = CGPointMake(newCenterX, self.heading.center.y);
}];
0
azamatikus On

Try to use CGAffineTransformMakeTranslation:

      [UIView animateKeyframesWithDuration:5
                               delay:0
                               options:UIViewKeyframeAnimationOptionCalculationModeLinear
                               animations:^{

                               view.transform = CGAffineTransformMakeTranslation(550, -40);
                               }];