Tengo un scrollview con paginación. En viewDidLoad, compruebo si la orientación actual es horizontal, a continuación, establezco su tamaño del contenido "s altura 440
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages,340)];
}
else if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))
{
[scroll setFrame:CGRectMake(0,0,480,480)];
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 440)];
}
todo funciona bien scrollview scrolls smoothy y no hay desplazamiento diagonal.
pero cuando cambia la orientación,
Tengo que configurar el cuadro de scrollview y el tamaño del contenido de nuevo y lo estoy configurando como sigue
-(void)orientationChanged:(id)object
{
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
self.scroll.frame = [[UIScreen mainScreen]bounds];
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 340)];
}
else
{
self.scroll.frame = CGRectMake(0,0,480,480);
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 600)];
}
}
No puedo entender por qué tengo que configurar el contenidola altura del tamaño hasta 600 en modo horizontal, y eso tampoco es suficiente. Y agrega un problema más que scrollview comienza a desplazarse en diagonal, lo que no quiero ya que me parece tan extraño. ¿Alguien puede ayudarme a entender dónde y qué me estoy perdiendo?
He puesto la máscara de autorizing de scrollview como
[scroll setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight];
Pero cambiarlo no ayuda.
Respuestas
2 para la respuesta № 1Aquí hay un problema en su código. ¿Por qué estás configurando el tamaño de marco como este? Tienes solo el tamaño de pantalla de 320px width
. Y cuando cambia a landscape
, la altura será única 320px
. Pero estas configurando el pergamino. height
como 480px
y eso goes out of the screen
y start to scroll diagonally
.
self.scroll.frame = CGRectMake(0,0,480,480);
En lugar de ese tamaño de marco, cambia así
self.scroll.frame = CGRectMake(0,0,480,320);
Y necesitas configurar el tamaño del contenido depende de la contenido estas teniendo dentro del vista de desplazamiento en cualquier orientación.
5 para la respuesta № 2
No lo usas
UIDeviceOrientation
. UtilizarUIInterfaceOrientation
en lugar.DeviceOrientation
tiene dos opciones adicionales que no necesitas aquí.UIDeviceOrientationFaceUp
yUIDeviceOrientationFaceDown
)Regreso
Yes
deshouldAutorotateToInterfaceOrientation
Ahora
willRotateToInterfaceOrientation: duration:
será llamado cada vez que gire su dispositivo.Implementar este método de esta manera.
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { CGRect frame; int pageNumber = 2; int statusBarHeight = 20; if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) { frame = CGRectMake(0, 0, 480, 320 - statusBarHeight); } else { frame = CGRectMake(0, 0, 320, 480 - statusBarHeight); } scrollView.frame = frame; scrollView.contentSize = CGSizeMake(frame.size.width * 2, frame.size.height); }
Dejar,
pageNumber = 2
statusBarHeight = 20