一个简单的例子:
1.注册通知:
//设备方向消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(GuomobWallonDeviceOrientationChange) name:
UIDeviceOrientationDidChangeNotification object:nil];
2.消息响应
-(void)GuomobWallonDeviceOrientationChange
{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
//if((orientation==UIDeviceOrientationPortrait)||(orientation==UIDeviceOrientationPortraitUpsideDown))
if(orientation==UIDeviceOrientationPortrait)
{
// NSLog(@"竖屏***************");
imageview.frame=CGRectMake(0, 0, rect_screen.size.width, rect_screen.size.height);
btnReqAd.frame=CGRectMake((rect_screen.size.width-300)/2, 40, 300, 60);
btnReadPoint.frame=CGRectMake((rect_screen.size.width-300)/2, 105, 300, 40);
btnWritePoint.frame=CGRectMake((rect_screen.size.width-300)/2, 150, 300, 40);
btncheckPoint.frame=CGRectMake((rect_screen.size.width-300)/2, 195, 300, 40);
btnupdatePoint.frame=CGRectMake((rect_screen.size.width-300)/2, 240, 300, 40);
}
else if((orientation==UIDeviceOrientationLandscapeLeft)||(orientation==UIDeviceOrientationLandscapeRight))
{
// NSLog(@"横屏***************");
imageview.frame=CGRectMake(0, 0, rect_screen.size.height, rect_screen.size.width);
btnReqAd.frame=CGRectMake((rect_screen.size.height-300)/2, 40, 300, 60);
btnReadPoint.frame=CGRectMake((rect_screen.size.height-300)/2,105, 300, 40);
btnWritePoint.frame=CGRectMake((rect_screen.size.height-300)/2, 150, 300, 40);
btncheckPoint.frame=CGRectMake((rect_screen.size.height-300)/2, 195, 300, 40);
btnupdatePoint.frame=CGRectMake((rect_screen.size.height-300)/2, 240, 300, 40);
}
}
本文介绍了一个简单的iOS应用示例,展示了如何通过NSNotification来监听设备的方向变化,并根据不同方向调整UI布局,确保应用在竖屏和横屏下都有良好的显示效果。
261

被折叠的 条评论
为什么被折叠?



