Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Orientation get future self.view.bounds

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.

like image 734
Byte Avatar asked Dec 17 '22 00:12

Byte


2 Answers

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.

like image 148
MishieMoo Avatar answered Jan 01 '23 23:01

MishieMoo


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

like image 38
liamnichols Avatar answered Jan 01 '23 22:01

liamnichols