2009-11-06 8 views
10

Así que todo lo que quiero hacer es reproducir un sonido cuando el usuario toca un UIScrollView. El UIScrollViewDelegate tiene que scrollViewWillBeginDragging: método, sino que sólo se pidió a touchMoved. Quiero que me llamen en touchBegan.UIScrollView touchesBegan

Toques de pruebaComienzo: withEvent: pero no recibe ningún toque. ¿Alguien tiene una pista?

Respuesta

6

Creo que tendrá que subclase UIScrollView para poder hacer esto.

toquesBegan: withEvent: solo se envía a las subclases de UIView. Probablemente estás implementando toques Bean: withEvent: en tu controlador, ¿no? Si es así, no funcionará desde allí ...

Alternativamente, si coloca una subclase de UIView (que escribió) en su UIScrollView, también puede ver el evento touchesBegan desde allí (pero solo cuando el usuario toca esa subvista particular). UIScrollView pasa en toques a sus subvistas por defecto (ver touchesShouldBegin: withEvent: inContentView: en UIScrollView).

0

Debe agregar toques Comenzar: conEvent: en su UIScrollView, no en UIScrollViewDelegate. UIScrollView es una subclase de UIResponder, que tiene los eventos táctiles.

+0

¿Y si el 'UIScrollView' ** ** ES el' UIScrollViewDelegate'? – aleclarson

5

También puede responder a estos eventos en su controlador. Para que eso funcione, tiene que definir esta función (en el controlador):

- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 

Luego, en 'viewDidAppear', es necesario llamar 'becomeFirstResponder':

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    [self becomeFirstResponder]; 
1

nuevas actualizaciones en el número here (incluido un enlace al código fuente de ZoomScrollView + alguna explicación excelente sobre las funciones internas de UIScrollView). Además, consulte el ejemplo actualizado de Apple ScrollViewSuite.

+2

¡El ejemplo de ScrollViewSuite es enorme! ¡Quiero decir sangriento 74 MB! : O – tipycalFlow

13

Utilice un golpecito Gesto reconocedor de lugar:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)]; 
[recognizer setNumberOfTapsRequired:1]; 
[recognizer setNumberOfTouchesRequired:1]; 
[scrollView addGestureRecognizer:recognizer]; 
+0

Ok, esto funciona, pero ¿Cómo debo declarar el "toque" Método para recibir el lugar donde tocó el usuario? –

+0

@HernanArber Por ejemplo: http: // stackoverflow.com/preguntas/8721864/conseguir-Screen-coordenadas-de-uilongpressgesturerecognizer – Roosevelt

18

Utilice un golpecito Gesto reconocedor de lugar:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)]; 
[recognizer setNumberOfTapsRequired:1]; 
[recognizer setNumberOfTouchesRequired:1]; 
[scrollView addGestureRecognizer:recognizer]; 

O

maquillaje subclase de su UIScrollView y aplicar todos los

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

// If not dragging, send event to next responder 
    if (!self.dragging){ 
    [self.nextResponder touchesBegan: touches withEvent:event]; 
    } 
    else{ 
    [super touchesEnded: touches withEvent: event]; 
    } 
} 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

// If not dragging, send event to next responder 
    if (!self.dragging){ 
    [self.nextResponder touchesBegan: touches withEvent:event]; 
    } 
    else{ 
    [super touchesEnded: touches withEvent: event]; 
    } 
} 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 

    // If not dragging, send event to next responder 
    if (!self.dragging){ 
    [self.nextResponder touchesBegan: touches withEvent:event]; 
    } 
    else{ 
    [super touchesEnded: touches withEvent: event]; 
    } 
} 
+1

he añadido el código de la Categoría en lugar de subclases, y funcionó ... –

+0

@ Rajneesh071 donde está manejando el booleano "arrastrando"? ¿se maneja automáticamente cuando se subclasifica 'UIScorllView'? –

+0

@ Tío jeet.chanchawat su subclase de su UIScrollView y arrastre es propiedad booleana de UIScrollView – Rajneesh071

0

Rajneesh071 rápida respuesta en 4 Cambiar la clase personalizada de ScrollView en guión gráfico a CustomScrollView

import Foundation 

class CustomScrollView: UIScrollView { 
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     if !isDragging { 
      next?.touchesBegan(touches, with: event) 
     } else { 
     super.touchesBegan(touches, with: event) 
     } 
    } 
} 
0

Es necesario subclase vista de desplazamiento y luego poner en práctica el touchesBegan method.You es necesario también establecer la propiedad delayTouchDown a falso.