2009-11-06 13 views
10

Quindi tutto ciò che voglio fare è riprodurre un suono quando l'utente tocca un UIScrollView. UIScrollViewDelegate ha quel metodo scrollViewWillBeginDragging: ma viene chiamato solo su touchMoved. Voglio che venga chiamato su touchBegan.UIScrollView touchesBegan

Tentativi toccati in tavola: conEvent: ma non riceve alcun tocco. Qualcuno ha un indizio?

risposta

6

Penso che sarà necessario sottoclasse UIScrollView per poterlo fare.

touchBurn: withEvent: viene inviato solo a sottoclassi di UIView. Stai probabilmente implementando i touchBegan: withEvent: nel tuo controller, giusto? Se è così, non funzionerà da lì ...

In alternativa, se metti una sottoclasse di UIView (che hai scritto) nel tuo UIScrollView, puoi anche prendere il tocco Evento Began da lì (ma solo quando l'utente tocca quella particolare sottoview). UIScrollView passa i tocchi alle sue visualizzazioni secondarie per impostazione predefinita (vedere i touchShouldBegin: withEvent: inContentView: in UIScrollView).

0

È necessario aggiungere tocchi Began: withEvent: sul proprio UIScrollView, non su UIScrollViewDelegate. UIScrollView è una sottoclasse di UIResponder, che ha gli eventi touch.

+0

Cosa succede se 'UIScrollView' ** È ** il' UIScrollViewDelegate'? – aleclarson

5

È inoltre possibile rispondere a questi eventi nel controller. Per funzionare, è necessario definire questa funzione (nel controller):

- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 

Poi, nel 'viewDidAppear', è necessario chiamare 'becomeFirstResponder':

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

Nuovi aggiornamenti sul problema here (incluso un collegamento al codice sorgente ZoomScrollView + qualche spiegazione eccellente sugli interni di UIScrollView). Inoltre, controlla l'esempio aggiornato di Apple ScrollViewSuite.

+2

L'esempio di ScrollViewSuite è enorme !!! Intendo maledettamente 74 MB! : O – tipycalFlow

13

Utilizzare un Tap Gesture Recognizer invece:

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

Ok, funziona, ma come dovrei dichiarare il metodo "touch" per ricevere il luogo in cui l'utente è stato toccato? –

+0

@HernanArber Ad esempio: http: // stackoverflow.it/questions/8721864/getting-screen-coordinates-from-uilongpressgesturerecognizer – Roosevelt

18

Utilizzare un Tap Gesture Recognizer invece:

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

O

make sottoclasse del vostro UIScrollView e realizzare tutte

-(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

Ho aggiunto il tuo codice come categoria invece di sottoclasse e ha funzionato ... –

+0

@ Rajneesh071 dove stai gestendo il "trascinamento" booleano? è gestito automaticamente quando sottoclasse 'UIScorllView'? –

+0

@ jeet.chanchawat Dude la sottoclassella del tuo UIScrollView e il trascinamento è proprietà booleane di UIScrollView – Rajneesh071

0

Rajneesh071 risposta rapida 4 Modificare la classe personalizzata di ScrollView in storyboard per 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

È necessario sottoclasse vista di scorrimento e quindi implementare la touchesBegan method.You necessità anche impostare la proprietà delayTouchDown su false.