Afficher un Label après un "long press", et le supprimer ensuite...

Bonjour,

Je suis en train de m'attaquer à  l'utilisation des gestes... et après m'être pas mal torturé les neurones, j'ai enfin réussi à  faire la première partie...

En fait, j'ai une MKMapView, sur laquelle j'ai ajouté quelques MKOverlay (5 ou 6) qui sont principalement des polygones. Je souhaiterais, lorsque l'on fait un appui long, afficher le titre du polygone touché (s'il y en a un), mais je souhaiterais que ce label disparaisse lorsque l'on relâche le touch...



J'ai réussi à  identifier s'il y a un polygone sous le doigt, et à  afficher le label, mais je ne vois pas comment le faire disparaà®tre ensuite. En fait, je ne sois pas utiliser le bon geste...

Voici mon code :
<br />
-(IBAction)handleGesture {<br />
	CGPoint location = [self.longPressGestureRecognizer locationInView:self.view];<br />
	CLLocationCoordinate2D coordinate = [self.myMap convertPoint:location toCoordinateFromView:self.view];<br />
	MKMapPoint mapPoint = MKMapPointForCoordinate(coordinate);<br />
	BOOL mapCoordinateIsInPolygon = FALSE;<br />
	for (MKPolygon *region in self.myMap.overlays){<br />
		MKPolygonView *polygonView = (MKPolygonView *)[self.myMap viewForOverlay:region];<br />
		CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];<br />
		mapCoordinateIsInPolygon = CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);<br />
		if (mapCoordinateIsInPolygon) {<br />
			UILabel *myLabel=[[UILabel alloc] initWithFrame:CGRectMake(location.x, location.y, 100, 30)];<br />
			myLabel.text=[[self.myOverlaysDescription objectAtIndex:[[self.myMap overlays] indexOfObject:[polygonView overlay]]] objectForKey:@&quot;Title&quot;];<br />
			myLabel.textColor=[UIColor redColor];<br />
			myLabel.backgroundColor=[UIColor clearColor];<br />
			myLabel.font=[UIFont systemFontOfSize:12];<br />
			[self.myMap addSubview:myLabel];<br />
			[myLabel release];<br />
		}<br />
	}<br />
}<br />


image/crazy.gif' class='bbc_emoticon' alt=' B) ' />

Quelqu'un a-t-il une petite piste ? Merci d'avance...

Réponses

  • Toutes les gestures ont une propriété state contenant une constante de type UIGestureRecognizerState, identifiant la nature de l'événement.



    valeur UIGestureRecognizerStateBegan => la gesture vient de se produire

    valeur UIGestureRecognizerStateEnded => la gesture vient de se TERMINER
  • Oui, j'ai vu cela... J'ai essayé de faire apparaitre mon label lorsque UIGestureRecognizerStateBegan et de la supprimer lorsque UIGestureRecognizerStateEnded... mais rien ne se passe.

    Merci en tout cas pour ton aide. Je poursuis mes essais !
  • Ah, çà  y est, j'avais du faire une boulette lorsque j'ai utilisé les "state", car maintenant çà  fonctionne !



    Voila ce que j'ai finalement fait, si çà  peut servir à  quelqu'un...
    <br />
    -(IBAction)handleGesture {<br />
    	if (self.longPressGestureRecognizer.state==UIGestureRecognizerStateBegan) {<br />
    		CGPoint location = [self.longPressGestureRecognizer locationInView:self.view];<br />
    		CLLocationCoordinate2D coordinate = [self.myMap convertPoint:location toCoordinateFromView:self.view];<br />
    		MKMapPoint mapPoint = MKMapPointForCoordinate(coordinate);<br />
    		BOOL mapCoordinateIsInPolygon = FALSE;<br />
    		for (MKPolygon *region in self.myMap.overlays){<br />
    			MKPolygonView *polygonView = (MKPolygonView *)[self.myMap viewForOverlay:region];<br />
    			CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];<br />
    			mapCoordinateIsInPolygon = CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);<br />
    			if (mapCoordinateIsInPolygon) {<br />
    				UILabel *myLabel=[[UILabel alloc] initWithFrame:CGRectMake(location.x, location.y, 100, 30)];<br />
    				myLabel.text=[[self.myOverlaysDescription objectAtIndex:[[self.myMap overlays] indexOfObject:[polygonView overlay]]] objectForKey:@&quot;Title&quot;];<br />
    				myLabel.backgroundColor=[UIColor clearColor];<br />
    				myLabel.font=[UIFont systemFontOfSize:12];<br />
    				[self.myMap addSubview:myLabel];<br />
    				[myLabel release];<br />
    			}<br />
    		}<br />
    	}<br />
    	if (self.longPressGestureRecognizer.state==UIGestureRecognizerStateEnded) {<br />
    		for (UILabel *theLabel in self.myMap.subviews) {<br />
    			if ([theLabel isKindOfClass:[UILabel class]]) {<br />
    				[theLabel removeFromSuperview];<br />
    			}<br />
    		}<br />
    	}<br />
    }<br />
    




    Si vous avez des remarques, ou améliorations éventuelles...



    Merci Draken, tu m'as remis sur la voie que j'avais abandonné ! Pour la peine, je vais boire un Perrier Citron à  ta santé !! image/cliccool.gif' class='bbc_emoticon' alt=' :p ' />
  • image/eddy58.gif' class='bbc_emoticon' alt=' :p ' />
Connectez-vous ou Inscrivez-vous pour répondre.