0

我想知道如何知道用户最后一次触摸哪个标签以及如何从视图中删除该特定标签。我知道如何更改所有标签的属性,但我不知道如何找出最后选择的标签。

这就是我向视图添加标签的方式。

myNewLabel.text =textField.text;
numberOfLabels++;
myNewLabel.tag=numberOfLabels;
[self.view addSubview:myNewLabel];
[shirtBackgroundView addSubview:myNewLabel];
[myNewLabel addGestureRecognizer:panGestureRecognizer];
[myNewLabel addGestureRecognizer:rotateGestureRecognizer];
[myNewLabel addGestureRecognizer:PinchGestureRecognizer];
myNewLabel.userInteractionEnabled=YES;
myNewLabel.backgroundColor=[UIColor clearColor]; 
[arrayForLabels addObject:myNewLabel];   

这是我更改所有标签颜色的方法。

    for(int i=0;i<numberOfLabels;i++)
    {
        UILabel *tempLabel = [arrayForLabels objectAtIndex:i];
        tempLabel.textColor=[UIColor redColor];
    }



 -(void)labelMoved:(UIPanGestureRecognizer *)recognizer
{
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];



    if (recognizer.state == UIGestureRecognizerStateEnded) {

        CGPoint velocity = [recognizer velocityInView:self.view];
        CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
        CGFloat slideMult = magnitude / 200;
        NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);

        float slideFactor = 0.1 * slideMult; // Increase for more of a slide
        CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
                                         recognizer.view.center.y + (velocity.y * slideFactor));
        finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
        finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);

        [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            recognizer.view.center = finalPoint;
        } completion:nil];


    }

}

感谢您的任何建议...

4

2 回答 2

1

您可以将 .h 类中的属性声明为 UILabel *lastSelectedLabel; 在你的识别器中

-(void)labelMoved:(UIPanGestureRecognizer *)recognizer{
          if(lastSelectedLabel!=nil){
                [lastSelectedLabel removeFromSuperview];
                lastSelectedLabel=recognizer.view;

          }else
              lastSelectedLabel=recognizer.view;
}

我认为上面应该工作..

谢谢。

于 2013-02-08T06:28:49.123 回答
1

对于检测UIlabels以捕获水龙头:

label.userInteractionEnabled = YES;
 UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecieved:)]; 
[label addGestureRecognizer:tap];
[tap release];

//当你触摸时重复每个额外的标签

-(void) tapRecieved:(UITapGestureRecognizer *)tap{  
      currentLabel = (UILabel *) tap.view;
        [currentLabel removeFromSuperview];
 }
于 2013-02-08T06:32:14.543 回答