Why doesn't this UIView layout code work as I want?
Background:
- I have a custom UIView I have in my UIViewController
- The custom UIView has a clock background imageview and an hourhand imageview
- After introducing some code (see below) to try to resize the hour hand for an orientation change I'm getting stuck.
- The code below really stuffs things up - the border outlike of the hour glass hand is way off with this code
Any ideas what I'm doing wrong - there's obvious some assumption or misunderstanding I have for it to screw up like this...
Code called by UIViewController (in "didRotateFromInterfaceOrientation" method) to my custom view:
// Centre image
self.hourHandImageView.center = self.hourFaceUIImageView.center;
// Resize to cater for either Portrait or Landscape orientation
CGSize currSize = self.hourFaceUIImageView.frame.size;
float newHourHandImageheightWidth = currSize.width < currSize.height ? currSize.width : currSize.height;
CGSize newHourHan开发者_运维百科dViewSize = CGSizeMake(newHourHandImageheightWidth, newHourHandImageheightWidth);
CGRect newRect = self.hourHandImageView.frame;
newRect.size = newHourHandViewSize;
self.hourHandImageView.frame = newRect;
PS. Without the "Resize to cater for either Portrait or Landscape orientation" code the hour hand correctly stays centered where it should (noting I use transformation to turn it around). There all I really need to do is resize the hour hand appropriately after an orientation change, noting the hour background image is smaller in the landscape mode.
First, how can you tell if you are in portrait or landscape orientation? With this (?):
float newHourHandImageheightWidth = currSize.width < currSize.height ? currSize.width : currSize.height;
What i would advice you, is to do something like this:
-
(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
[self reOrderToPortrait];
} else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
[self reOrderToLandScape];
}
}
Then on each method, you should define the new frames for your views. Start by removing your autosizing from your views, because you will define the new frames, and you dont want that to interfere with. You could also define some mesures like this:
#define hourHandImageViewPortrait CGPointMake(...,...)
#define hourHandImageViewLandScape CGPointMake(..., ...)
精彩评论