handleLongPressGesture を使用して注釈を追加し、MapView の隣にテキスト フィールドを作成しています。私の問題は、サブビューを挿入するときに関数が一度だけ呼び出されることです:
[self createTextfieldForPin:myString :x :y];
誰か知っていますか、なぜですか?その関数がないと、ロンドプレスをしているときに毎回 handleLongPressGesture 関数が呼び出されます...
ここに私の完全なコードがあります:
-(void)handleLongPressGesture:(UIGestureRecognizer*)sender
{
// This is important if you only want to receive one tap and hold event
if (sender.state == UIGestureRecognizerStateEnded)
{
//[self.mapView removeGestureRecognizer:sender];
// [self.mapView removeAnnotations:[self.mapView annotations]];
}
else
{
// Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
CGPoint point = [sender locationInView:self.mapView];
CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
// Then all you have to do is create the annotation and add it to the map
UserAnnotation *dropPin = [[UserAnnotation alloc] initWithCoordinate:locCoord];
[self.mapView addAnnotation:dropPin];
//create NSString from coordinate
NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
[fmt setMaximumFractionDigits:2];
NSString *myString = [[NSString alloc]initWithFormat:@"%@ / %@",[fmt stringFromNumber:[NSNumber numberWithFloat:locCoord.latitude]],[fmt stringFromNumber:[NSNumber numberWithFloat:locCoord.longitude]]];
[self createTextfieldForPin:myString :x :y];
}
}
**編集
-(void)createTextfieldForPin:(NSString *)coordinates: (CGFloat) _x: (CGFloat) _y
{
self.label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 100.0, 21.0)];
self.label.text = coord;
y = y + 25.0;
self.textfield = [[UITextField alloc]initWithFrame:CGRectMake(x,y,163,31)];
self.textfield.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.label];
[self.view addSubview:self.textfield];
}
**編集
ピンを追加するたびに、ラベル (座標用) とテキストフィールドを作成して、都市または sth を追加したいと考えています。写真のように。しかし、createTextfieldForPin がその関数内にあるときに、handleLongPressGesture を 1 回だけ呼び出すことができます。理由はわかりません。