2012-07-19 15 views
22

Quiero que UIScrollView se desplace rápidamente después del barrido rápido, like this. Aunque, cuando se activa la búsqueda, el desplazamiento funciona one page at a time. ¿Es posible desplazarse rápidamente, cuando la búsqueda está habilitada con un simple movimiento de dedo sin una implementación manual utilizando reconocedores de gestos?Sensibilidad/velocidad de desplazamiento de UIScrollView con paginación

+0

¿Alguna vez encontró una solución a esto? – Eric

+0

Ojalá hubiera una buena solución para esto. Parece que hay dos formas de implementar esto: implementar manualmente reconocedores de gestos o implementar paginación manualmente. – UrK

+0

Sí Es posible que lo haya implementado antes de 6 meses. puedes hacer manualmente los reconocedores de gestos ... – JohnWhite

Respuesta

40

Esto es bastante sencillo, sin embargo, debe implementar el aspecto de paginación usted mismo (que es bastante simple). No necesitas reconocedores de gestos.

Swift

En primer lugar, ajuste su UIScrollView tasa de desaceleración:

scrollView.decelerationRate = UIScrollViewDecelerationRateFast 

Asumamos que tenemos una gran variedad de contenidos - llamémoslo yourPagesArray. En su UIScrollViewDelegate, aplicar el siguiente método:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) 
{ 
    //This is the index of the "page" that we will be landing at 
    let nearestIndex = Int(CGFloat(targetContentOffset.pointee.x)/scrollView.bounds.size.width + 0.5) 

    //Just to make sure we don't scroll past your content 
    let clampedIndex = max(min(nearestIndex, yourPagesArray.count - 1), 0) 

    //This is the actual x position in the scroll view 
    var xOffset = CGFloat(clampedIndex) * scrollView.bounds.size.width 

    //I've found that scroll views will "stick" unless this is done 
    xOffset = xOffset == 0.0 ? 1.0 : xOffset 

    //Tell the scroll view to land on our page 
    targetContentOffset.pointee.x = xOffset 
} 

También tendrá que aplicar el siguiente método delegado, para manejar el caso en que el usuario levanta su dedo suavemente sin causar ningún desaceleración de desplazamiento:

(actualización:.. es posible que no tenga que hacer esto bajo el último SDK de iOS parece que el método anterior delegado ahora se llama cuando hay velocidad cero)

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) 
{ 
    if !decelerate 
    { 
     let currentIndex = floor(scrollView.contentOffset.x/scrollView.bounds.size.width) 

     let offset = CGPoint(x: scrollView.bounds.size.width * currentIndex, y: 0) 

     scrollView.setContentOffset(offset, animated: true) 
    } 
} 

Objective-C

Configuración de la velocidad de desaceleración:

scrollView.decelerationRate = UIScrollViewDecelerationRateFast; 

Luego, su desplazamiento implantación vista delegado:

- (void) scrollViewWillEndDragging:(UIScrollView *)scroll withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset 
{ 
    //This is the index of the "page" that we will be landing at 
    NSUInteger nearestIndex = (NSUInteger)(targetContentOffset->x/scrollView.bounds.size.width + 0.5f); 

    //Just to make sure we don't scroll past your content 
    nearestIndex = MAX(MIN(nearestIndex, yourPagesArray.count - 1), 0); 

    //This is the actual x position in the scroll view 
    CGFloat xOffset = nearestIndex * scrollView.bounds.size.width; 

    //I've found that scroll views will "stick" unless this is done 
    xOffset = xOffset==0?1:xOffset; 

    //Tell the scroll view to land on our page 
    *targetContentOffset = CGPointMake(xOffset, targetContentOffset->y); 
} 

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{ 
    if(!decelerate) 
    { 
     NSUInteger currentIndex = (NSUInteger)(scrollView.contentOffset.x/scrollView.bounds.size.width); 

     [scrollView setContentOffset:CGPointMake(scrollView.bounds.size.width * currentIndex, 0) animated:YES]; 
    } 
} 
1

creo que podemos establecer scrollView.userInteractionEnabled = NO cuando nuestro dedo que toque. Y luego, cuando la animación se detenga, ábrela. Funciona para mí. Espero que te ayude.

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ 
    scrollView.userInteractionEnabled = NO; 
} 

// at the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 
    scrollView.userInteractionEnabled = YES; 
} 
Cuestiones relacionadas