ios objective-c uiscrollview scroll

ios - Desplazar de forma programada un UIScrollView



objective-c (8)

Con animación en Swift

scrollView.setContentOffset(CGPointMake(x, y), animated: true)

Tengo un UIScrollView que tiene varias vistas. Cuando un usuario mueve su dedo, la vista se desplaza hacia la derecha o hacia la izquierda dependiendo de la dirección del deslizamiento del dedo. Básicamente, mi código funciona de una manera similar a la aplicación de fotos de iPhone. Ahora, ¿hay alguna manera de que pueda hacer lo mismo programáticamente para que termine con una presentación de diapositivas que se ejecute por sí sola con un clic de un botón y una pausa configurable entre cada desplazamiento?

¿Cómo se hacen presentaciones de diapositivas con UIScrollView ?


Otra forma es

scrollView.contentOffset = CGPointMake(x,y);


Puede desplazarse a algún punto en una vista de desplazamiento con una de las siguientes afirmaciones en Objective-C

[scrollView scrollRectToVisible:CGRectMake(x, y, 1, 1) animated:YES]; // or [scrollView setContentOffset:CGPointMake(x, y) animated:YES];

o Swift

scrollView.scrollRectToVisible(CGRect(x: x, y: y, width: 1, height: 1), animated: true) // or scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)

Consulte la guía " Desplazamiento por el contenido de la vista de desplazamiento " desde Apple también .

Para hacer presentaciones de diapositivas con UIScrollView , organiza todas las imágenes en la vista de desplazamiento, configura un temporizador repetido, luego -setContentOffset:animated: cuando el temporizador se dispara.

Pero un enfoque más eficiente es utilizar 2 vistas de imágenes e intercambiarlas mediante transiciones o simplemente cambiar de lugar cuando se dispara el temporizador. Ver presentación de diapositivas de iPhone para más detalles.


Si desea controlar la duración y el estilo de la animación, puede hacer lo siguiente:

[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ scrollView.contentOffset = CGPointMake(x, y); } completion:NULL];

¡Ajuste la duración ( 2.0f ) y las opciones ( UIViewAnimationOptionCurveLinear ) para probar!


Swift 3

let point = CGPoint(x: 0, y: 200) // 200 or any value you like. scrollView.contentOffset = point


- (void)viewDidLoad { [super viewDidLoad]; board=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, 80)]; board.backgroundColor=[UIColor greenColor]; [self.view addSubview:board]; // Do any additional setup after loading the view. } -(void)viewDidLayoutSubviews { NSString *str=@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; index=1; for (int i=0; i<20; i++) { UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(-50, 15, 50, 50)]; lbl.tag=i+1; lbl.text=[NSString stringWithFormat:@"%c",[str characterAtIndex:arc4random()%str.length]]; lbl.textColor=[UIColor darkGrayColor]; lbl.textAlignment=NSTextAlignmentCenter; lbl.font=[UIFont systemFontOfSize:40]; lbl.layer.borderWidth=1; lbl.layer.borderColor=[UIColor blackColor].CGColor; [board addSubview:lbl]; } [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(CallAnimation) userInfo:nil repeats:YES]; NSLog(@"%d",[board subviews].count); } -(void)CallAnimation { if (index>20) { index=1; } UIView *aView=[board viewWithTag:index]; [self doAnimation:aView]; index++; NSLog(@"%d",index); } -(void)doAnimation:(UIView*)aView { [UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ aView.frame=CGRectMake(self.view.frame.size.height, 15, 50, 50); } completion:^(BOOL isDone) { if (isDone) { //do Somthing aView.frame=CGRectMake(-50, 15, 50, 50); } }]; }


[Scrollview setContentOffset:CGPointMake(x, y) animated:YES];


scrollView.setContentOffset(CGPoint(x: y, y: x), animated: true)