Domanda

Proprio giocando con l'SDK e mi chiedevo, se possibile, un evento UITouch può lavorare all'interno di un UIScrollView.

Ho installato un UIScrollView che gestisce una grande UIView, all'interno del UIView è un UIImageView, sono riuscito a ottenere l'UITouch per trascinare l'esterno UIImageView del UIScrollView ma dentro non sta registrando l'evento.

Credo che quello che stavo cercando di realizzare è stato trascinando l'UIImageView intorno alla grande UIView mentre i UIScrollView si muove lungo l'immagine se l'utente trascina oltre la POS di quando l'UIView quando l'UIImageView iniziato è il trascinamento, se questo ha un senso?

Molte grazie

È stato utile?

Soluzione

(se ho capito correttamente alla domanda)

UIScrollView intercetta eventi touchMoved e non li propaga al suo contenuto, se lo scorrimento è abilitata. Quindi, per fare il trascinamento nei contenuti UIScrollView nella mia app ho fatto il seguente trucco:

touchesBegan: Controlla se si tocca la regione "trascinabile". Se SI - scrolling disabilitare in UIScrollView.

touchesMoved:. Visualizza ora lo scorrimento è disattivata i tuoi contenuti riceve questo evento e si può spostare l'UIImageView trascinabili conseguenza

touchesEnded:. scorrimento Riattivare in UIScrolliew

Se si vuole trascinare la vista al di fuori della zona di UIScrollView visibile potrebbe anche essere necessario controllare manualmente se si è in prossimità dei confini e di regolare manualmente compensati contenuto (non ho provato questo me stesso, ma penso che dovrebbe funzionare) .

Altri suggerimenti

Il modo migliore per implementare questo che ho trovato è stato quello di creare una sottoclasse della classe UIScrollView stesso e quindi aggiungere i tocchi cominciarono metodo di evento nella sottoclasse. Questo ha funzionato per me.

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

NSLog(@"DEBUG: Touches began" );

UITouch *touch = [[event allTouches] anyObject];

[super touchesBegan:touches withEvent:event];
}

(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

NSLog(@"DEBUG: Touches cancelled");

// Will be called if something happens - like the phone rings

UITouch *touch = [[event allTouches] anyObject];

[super touchesCancelled:touches withEvent:event];

}


(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

NSLog(@"DEBUG: Touches moved" );

UITouch *touch = [[event allTouches] anyObject];

[super touchesMoved:touches withEvent:event];

}

(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"DEBUG: Touches ending" );
//Get all the touches.
NSSet *allTouches = [event allTouches];

//Number of touches on the screen
switch ([allTouches count])
{
    case 1:
    {
        //Get the first touch.
        UITouch *touch = [[allTouches allObjects] objectAtIndex:0];

        switch([touch tapCount])
        {
            case 1://Single tap

                break;
            case 2://Double tap.

                break;
        }
    }
        break;
}
[super touchesEnded:touches withEvent:event];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top