0

I want to animate a view to a specified origin when ever someone touched it and then comeback to its original position whenever user touched it again. I am using AutoLayout in my storyboard to align my views. It first go upward with perfect position but does not come back to its original position perfectly. I store the staring origin value in a CGFloat variable in my viewDidLoad method and then re assign that object to bring back the view. Here is my code Animation for upward (This is working perfect)

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
CGRect frame = _pickLocationView.frame;
frame.origin.y = _cityLabelView.frame.origin.y;
_pickLocationView.frame = frame;
[UIView commitAnimations];

Animation for Downward. (This method has some issue)

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
CGRect frame = _pickLocationView.frame;
frame.origin.y = kDefaultOriginOfPickLocationView;
_pickLocationView.frame = frame;
[UIView commitAnimations];
Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52

2 Answers2

0

The frame is not guaranteed to have its final dimensions and position in viewDidLoad. Use viewDidLayoutSubviews to set your original values instead. Note that viewDidLayoutSubviews can be called multiple times if the bounds of your view changes.

In your case you could also just save the original value just before you start the first animation.

Erik Johansson
  • 1,188
  • 1
  • 8
  • 22
0

A suggestion: Use the method - [self setNeedsLayout] before the animation starts.

Soham Ray
  • 215
  • 2
  • 4
  • Should i just invoke the [self setNeedsLayout] method or override and then invoke? Sorry if it is a stupid question but i am new with iOS programming. – Syed Qamar Abbas Mar 21 '16 at 14:03
  • Use it before [UIView commitAnimation]. just write the above piece code in my answer. see if any changes takes place. if not then try using the method after commitAnimation. – Soham Ray Mar 21 '16 at 14:11