用户最后触摸的标签以及如何从视图中删除它

时间:2013-02-08 06:14:32

标签: ios nsmutablearray uilabel

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

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

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];


    }

}

感谢您的任何建议......

2 个答案:

答案 0 :(得分:1)

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

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

          }else
              lastSelectedLabel=recognizer.view;
}

我认为上面应该有用..

感谢。

答案 1 :(得分: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];
 }