2014-07-15 10 views
6

Sto provando a chiamare una casella di avviso quando tocco e tieni premuta un'immagine per 2 secondi. Ecco quello che ho ottenuto finora:Come implementare un tocco e tenere premuto su un UIImageView?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UILongPressGestureRecognizer *tapAndHoldGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAndHoldGesture:)]; 
    tapAndHoldGesture.minimumPressDuration = 0.1; 
    tapAndHoldGesture.allowableMovement = 600; 
    [self.view addGestureRecognizer:tapAndHoldGesture]; 
} 

- (void) handleTapAndHoldGesture:(UILongPressGestureRecognizer *)gestureRecognizer{ 
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) { 
     return; 
    } 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Gesture:" message:@"hold it" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 

Non so se questo effettua nulla, ma l'immagine View è a livello di codice creato più tardi e non sul carico. Grazie in anticipo, come ogni aiuto è apprezzato ..

Inoltre, ho guardato i seguenti link:

Long press gesture on UICollectionViewCell

Long press gesture recognizer on UIButton?

Apple Link 1

Apple Link 2

+0

Dove aggiungi gesture a imageView ?. Posso vedere solo il metodo del gestore –

+0

Il mio errore ... Nel viewDidLoad. Grazie. –

risposta

6
-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setupGesture]; 
} 

-(void) setupGesture 
{ 
    UILongPressGestureRecognizer *lpHandler = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleHoldGesture:)]; 
    lpHandler.minimumPressDuration = 1; //seconds 
    lpHandler.delegate = self; 
    //myUIImageViewInstance - replace for your instance/variable name 
    [**myUIImageViewInstance** addGestureRecognizer:lpHandler]; 
} 

- (void) handleHoldGesture:(UILongPressGestureRecognizer *)gesture 
{ 
    if(UIGestureRecognizerStateBegan == gesture.state) 
    { 
     // Called on start of gesture, do work here 
    } 

    if(UIGestureRecognizerStateChanged == gesture.state) 
    { 
     // Do repeated work here (repeats continuously) while finger is down 
    } 

    if(UIGestureRecognizerStateEnded == gesture.state) 
    { 
     // Do end work here when finger is lifted 
    } 

} 
+0

Ancora non funziona per me ... È importante che la mia immagine sia 22x22? –

+0

Io non la penso così. Controllare la proprietà userInteractionEnabled, dovrebbe essere SI (vero) Sulla base della Riferimento Classe: userInteractionEnabled Un valore booleano che determina se gli eventi utente vengono ignorati e rimossi dalla coda degli eventi. @property (nonatomic, getter = isUserInteractionEnabled) BOOL userInteractionEnabled Discussion Questa proprietà è ereditata dalla classe genitore UIView. Questa classe modifica il valore predefinito di questa proprietà su NO. – gabriel

+0

ha funzionato per me senza aggiungere la riga * lpHandler.delegate = self; * – LuAndre

2

UIImageViews per impostazione predefinita sono userInteractionEnabled = NO. Se aggiungi il tuo identificatore di gesti a un'istanza di UIImageView, assicurati di reimpostarlo su SÌ: myImageView.userInteractionEnabled = YES

Problemi correlati