我创建了图像视图并在该视图中设置了图像,并将图像视图添加为滚动视图的子视图(如幻灯片放映图像)。所以我想动态设置图像视图框架大小和滚动视图框架大小。我想在纵向和横向模式下设置不同的图像视图框架大小和滚动视图大小。那么如何在横向和纵向模式下设置不同的帧大小。
这是我的示例代码,
- (void)viewDidLoad {
[super viewDidLoad];
self.scroll = [[UIScrollView alloc] init];
self.scroll.delegate = self;
self.scroll.pagingEnabled = YES;
self.scroll.contentMode = UIViewContentModeScaleAspectFit;
self.scroll.autoresizingMask = ( UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth );
[self.view addSubview:self.scroll];
totalArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"dashboard_demo_2.png"],
[UIImage imageNamed:@"dashboard_demo_1.png"],
[UIImage imageNamed:@"dashboard_demo_2.png"],
[UIImage imageNamed:@"3.jpg"],
[UIImage imageNamed:@"4.jpg"],
[UIImage imageNamed:@"12.jpg"],
[UIImage imageNamed:@"5.jpg"],nil];
x = 0;
for(int i = 0; i< [totalArray count]; i++)
{
img = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, 320, 200)];
img.image =[totalArray objectAtIndex:i];
x = x+320; // in portait mode if the image sets correctly, but landscape mode it will x = x + 480;
[self.scroll addSubview:img];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait ||
orientation == UIDeviceOrientationPortraitUpsideDown )
{
[self portraitMode];
}
else if (orientation == UIDeviceOrientationLandscapeLeft ||
orientation == UIDeviceOrientationLandscapeRight )
{
[self LandscapeMode];
}
return YES;
}
-(void) portraitMode
{
//How to set the frame sizes.
self.scroll.frame = CGRectMake(0, 0, 320, 200);
img.frame = CGRectMake(x, 0, 320, 200);
scroll.contentSize = CGSizeMake(x, 200); // it doesn't set
}
-(void) LandscapeMode
{
//How to set the frame sizes
self.scroll.frame = CGRectMake(0, 0, 480, 320);
img.frame = CGRectMake(x, 0, 480, 200);
scroll.contentSize = CGSizeMake(x, 320); // it doesn't set
}
PLease帮助我。
感谢。
答案 0 :(得分:0)
嗨,
您可以注册到通知中心以更改屏幕方向。
注册通知中心的代码。
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(ScreenRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
实施ScreenRotate方法。
-(void) screenRotate:(NSNotificationCenter*) notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if( orientation == UIDeviceOrientationLandscapeLeft)
{
}
else if ( orientation == UIDeviceOrientationLandscapeRight )
{
}
else if ( orientation == UIDeviceOrientationPortrait )
{
}
}
取消注册通知中心的代码。
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:nil];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}