1

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

lostintranslation
  • 23,756
  • 50
  • 159
  • 262

2 Answers2

1

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);
}];
Gamma
  • 1,347
  • 11
  • 18
  • Doesn't explain how to get it initially off the screen. See my edit. – lostintranslation Jun 07 '15 at 23:13
  • 1
    @lostintranslation So you can't influence the position of the view programatically at all? Did you perchance enable [Auto Layout](http://i.imgur.com/sNQ99rK.png) for its superview, in the Storyboard or otherwise? If that's the case, any Auto Layout constraints applying to your view will prevent you from manipulating its frame yourself. – Gamma Jun 07 '15 at 23:35
  • Well that has to be it. Pretty sure I have auto layout on and constraints. Although that brings up all kinds of other questions on how to align and constrain views across different devices and landscape/portrait. So no way to use animations and have constraints? BTW thanks for the help, truly appreciated. – lostintranslation Jun 08 '15 at 02:31
  • Also animation does move the view. Not sure if that matters. But yeah initially setting the view to off screen does not work. – lostintranslation Jun 08 '15 at 03:58
0

Try to use CGAffineTransformMakeTranslation:

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

                               view.transform = CGAffineTransformMakeTranslation(550, -40);
                               }];
azamatikus
  • 73
  • 6