4

Background: I wanted to animate the change in my content along with the orientation. My content position is relative to the self.view.bounds.

Problem: In order to animate the content along with the bounds, I would need to know what would the bounds of the view be at the end. I can hard code it but I hope to find a more dynamic way.

Codes: So all animation takes place in willRotateToInterfaceOrientation as per following:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    //centering the image
    imageView.frame = CGRectMake(self.view.bounds.origin.x + (self.view.bounds.size.width - image.size.width)/2 , self.view.bounds.origin.y + (self.view.bounds.size.height - image.size.height)/2, image.size.width, image.size.height);
    NSLog(@"%d",[[UIDevice currentDevice] orientation]);
    NSLog(@"%f", self.view.bounds.origin.x);
    NSLog(@"%f", self.view.bounds.origin.y);
    NSLog(@"%f", self.view.bounds.size.width);
    NSLog(@"%f", self.view.bounds.size.height);

}

The bounds are before the orientation change. Thus, this only works if the transformation is 180 degrees. If I were to use didRotateFromInterfaceOrientation, the animation will be after the orientation change which looks awful.

Byte
  • 2,920
  • 3
  • 33
  • 55

3 Answers3

11

Use willAnimateRotationToInterfaceOrientation:duration: instead. This method gets called from within the rotation animation block, and all the bounds have been set correctly at this point. Docs.

MishieMoo
  • 6,620
  • 2
  • 25
  • 35
1

If you want it dynamic then when you initialize imageView, set the autoresizingMask property so that when the imageView's superview resizes on the rotate the margins can auto resize themselves...

 imageView = //init imageView
 imageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
 //addtional config

This means you only need to set the frame once then the imageView will always adjust itself to stay in the middle.

Check out the UIView class reference http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/uiview_class/uiview/uiview.html to see what else you can do with the autoresizingMask property

liamnichols
  • 12,419
  • 2
  • 43
  • 62
  • +1 for great idea. I will have to go for the answer above though, since it is actually what I am looking for. Thank you! – Byte May 02 '12 at 19:55
0

If I'm understanding you correctly, you just need to swap the X/Y coordinates and width/height in you're CGRectMake, to get your future layout for a 90 degree change. If it's 180 degree change, then it's the same as current

CGRectMake(self.view.bounds.origin.y + (self.view.bounds.size.height - image.size.height)/2 , self.view.bounds.origin.x + (self.view.bounds.size.width - image.size.width)/2, image.size.height, image.size.width);
JoeCortopassi
  • 5,083
  • 7
  • 37
  • 65
  • Thank you, this is however, quite a hack. Because then you would have to handle 180 degrees flip, etc. Not really worth it. Thank you for your thoughts. – Byte May 02 '12 at 20:00
  • Definitely a hack, but hacks have there place to. 3 second hack > than an hour on google if your on a deadline – JoeCortopassi May 02 '12 at 21:03