HandleLongPressGesture仅在添加子视图时调用一次

时间:2012-10-14 12:26:14

标签: ios ipad mkmapview uigesturerecognizer

我正在使用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];

}

**编辑

每当我添加一个图钉时,我还想创建一个标签(用于坐标)和一个文本字段来添加一个城市或者其他。 就像在图片上。但是当createTextfieldForPin在函数内部时,我只能调用handleLongPressGesture一次..我不知道为什么

enter image description here

0 个答案:

没有答案