当应用程序旋转时,准确地放置和移动视图的“正确”方式是什么?也就是说,当UI从纵向旋转到横向(反之亦然)时,我如何对视图的位置、大小和回流进行细粒度控制?我认为我的两个选择是:
如果您有两个具有足够不同布局和元素的视图,那么第一种方法可能就足够好了。如果您的两个视图在不同方向上的大小基本相同,那么第二种方法可能是只使用一个视图的更好方法。
我怀疑前者可以用IB来完成,而后者应该通过编程来完成。
发布于 2011-05-24 14:03:51
要在iPhone旋转时对子视图的位置和大小进行细粒度控制,请在UIViewController方法willAnimateRotationToInterfaceOrientation:duration:
中更改子视图的框架。此方法在动画块中调用,因此您在动画块中对子视图帧所做的所有更改都是动画的。
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
// Portrait frames
self.subviewA.frame = CGRectMake(x, y, width, height);
self.subviewB.frame = CGRectMake(x, y, width, height);
self.subviewC.frame = CGRectMake(x, y, width, height);
} else {
// Landscape frames
self.subviewA.frame = CGRectMake(x, y, width, height);
self.subviewB.frame = CGRectMake(x, y, width, height);
self.subviewC.frame = CGRectMake(x, y, width, height);
}
}
发布于 2011-05-24 10:50:00
这里有一个想法。我担心动画效果会很滑稽,不过你可以试试。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
duration:(NSTimeInterval)duration {
if (toOrientation == UIInterfaceOrientationLandscapeLeft ||
toOrientation == UIInterfaceOrientationLandscapeRight) {
[UIView beginAnimations:nil context:NULL]; {
[UIView setAnimationDuration:1.0];
}
[UIView setAnimationDelegate:self];
[viewA setFrame:CGRectMake(?,?,?,?)];
[viewB setFrame:CGRectMake(?,?,?,?)];
[viewC setFrame:CGRectMake(?,?,?,?)];
} [UIView commitAnimations];
} else if (toOrientation == UIInterfaceOrientationPortrait ||
toOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[UIView beginAnimations:nil context:NULL]; {
[UIView setAnimationDuration:1.0];
}
[UIView setAnimationDelegate:self];
[viewA setFrame:CGRectMake(?,?,?,?)];
[viewB setFrame:CGRectMake(?,?,?,?)];
[viewC setFrame:CGRectMake(?,?,?,?)];
} [UIView commitAnimations];
}
发布于 2011-05-25 16:57:09
我有一个想法,这对我来说是正确的,我在一个视图上使用一个小视图,当设备的方向改变时,它会自动旋转。使用一个通知程序,然后在方法中更改方向。
#define DegreesToRadians(x) (M_PI * x / 180.0)
.
[[NSNotificationCenter defaultCenter] addObserver:showIndicator
selector:@selector(setProperOreintation)
name:UIDeviceOrientationDidChangeNotification
object:nil];
.
-(void)setProperOrientation {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
if (orientation == UIDeviceOrientationPortraitUpsideDown)
self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, DegreesToRadians(180));
else if (orientation == UIDeviceOrientationLandscapeLeft)
self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, DegreesToRadians(90));
else if (orientation == UIDeviceOrientationLandscapeRight)
self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, DegreesToRadians(-90));
[UIView commitAnimations]; }
这里不是旋转,而是平移或缩放
https://stackoverflow.com/questions/6104963
复制相似问题