【发布时间】:2012-05-30 22:32:12
【问题描述】:
我是 ios 编程新手,遇到了一个错误。我的应用程序用于杂志,我使用 UIScrollView 来查看文章。
在我的应用程序中,我有 2 种方法在 UIScrollView 中导航,使用手势和 2 个按钮,因为我根据页面更改滚动视图的内容,并使用一种方法前进和一种后退,在其中我使用 If 来评估它是第一页还是最后一页,并根据情况禁用按钮。
我第一次使用此方法访问视图时一切正常,但第二次访问按钮失败但手势工作正常,从按钮或手势调用方法时有区别吗?
代码如下:
//Here is where I assign the method to the buttons and gesture
[articuloAnterior addTarget:self action:@selector(cambiarPaginaAnterior) forControlEvents:UIControlEventTouchUpInside];
[articuloSiguiente addTarget:self action:@selector(cambiarPaginaSiguiente) forControlEvents:UIControlEventTouchUpInside];
UISwipeGestureRecognizer *avanzarPagina = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(cambiarPaginaSiguiente)];
avanzarPagina.direction = UISwipeGestureRecognizerDirectionLeft;
UISwipeGestureRecognizer *regresaPagina = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(cambiarPaginaAnterior)];
regresaPagina.direction = UISwipeGestureRecognizerDirectionRight;
//Here are the functions
-(void)cambiarPaginaAnterior{
//This method checks if it is the first page, in case it isn't it go one page back
NSLog(@"Pagina actual %d de %d",paginaMostrada,[appDelegate.seccionActual.listadoArticulos count]-1);
if (0 != paginaActual) {
paginaMostrada = paginaActual - 1;
NSLog(@"Pagina actual %d de %d",paginaMostrada,[appDelegate.seccionActual.listadoArticulos count]-1);
[self cambiarLimites];
}
}
-(void)cambiarPaginaSiguiente{
//In this method I check the actual page (paginaActual) to see if it is the last one, if it isn't then it go forward
NSInteger limiteDePaginacion = [appDelegate.seccionActual.listadoArticulos count] - 1;
NSLog(@"Pagina actual %d de %d",paginaMostrada,limiteDePaginacion);
if (limiteDePaginacion != paginaActual) {
paginaMostrada = paginaActual + 1;
NSLog(@"Pagina actual %d de %d",paginaMostrada,limiteDePaginacion);
[self cambiarLimites];
}
}
-(void)cambiarLimites{
//Here i save the point where the user was located and save it in an array
NSValue *posicionActualDeLaVista = [NSValue valueWithCGPoint:CGPointMake(0, contenedorGeneral.contentOffset.y)];
[posicionDeLaPagina removeObjectAtIndex:paginaActual];
[posicionDeLaPagina insertObject:posicionActualDeLaVista atIndex:paginaActual];
//Here I change the value of the actual page
paginaActual=paginaMostrada;
//Here I disable the buttons or make them able
if (paginaActual==([appDelegate.seccionActual.listadoArticulos count]-1)) {
articuloSiguiente.enabled = NO;
}
else {
articuloSiguiente.enabled = YES;
}
if (paginaActual==0) {
articuloAnterior.enabled = NO;
}
else {
articuloAnterior.enabled = YES;
}
//A log to check the state of the buttons
NSLog(@"Pagina actual %d estado del boton anterior %@, estado del boton siguiente %@",paginaActual,articuloAnterior.enabled?@"yes":@"no",articuloSiguiente.enabled?@"yes":@"no");
//Acces an array with the points the user where
CGPoint puntoDeVisualizacion = [[posicionDeLaPagina objectAtIndex:paginaActual]CGPointValue];
//Here I remove the content of the uiscrollview and put the new one
[vistaTemporale removeFromSuperview];
vistaTemporale = [vistasParaElScroll objectAtIndex:paginaActual];
vistaTemporale = [self ResizeDeLaVista:vistaTemporale laPagina:paginaActual];
[contenedorGeneral addSubview:vistaTemporale];
[contenedorGeneral setContentOffset:puntoDeVisualizacion animated:NO];
//Check the view height and change the UIScrollView content height
float limitanteDeAltura;
limitanteDeAltura = [[appDelegate.viewSizes objectAtIndex:paginaActual]floatValue];
[contenedorGeneral setContentSize:CGSizeMake(anchoDeVista, limitanteDeAltura)];
}
我做了自己的手势,因为我需要取消对角滚动,所以我认为一次只显示一个视图是个好主意,但手势不是失败的,按钮没有被禁用,他们在 .h 文件中声明,并且在第一次访问时它们工作正常,知道它们为什么会失败吗?
【问题讨论】:
-
您提出问题的方式(即全部在一个长句中)让人难以理解,但如果我认为您使用 UIGestureRecognizer 和 UIButtons 来控制是正确的UIScrollView 的行为,你做错了。 UIScrollView 内置了手势识别,你不应该覆盖它。我并不是说没有使用按钮滚动它没有意义的情况,因为也许有,但听起来你对我来说过于复杂了......你能更清楚地解释你的确切内容吗想要发生吗?