1

我在 imageview 中添加了一个标签,并添加了用于拖动标签的 Pangesture。使用此代码标签在整体内拖动。但我只想将标签拖动限制在 uiimageview 的框架内。我怎样才能做到这一点?添加标签和手势的代码如下。

lblQuote=[[UILabel alloc]init];


        lblQuote.frame=CGRectMake(130,380, 400,100);
        lblQuote.userInteractionEnabled=TRUE;
        lblQuote.backgroundColor=[UIColor clearColor];
        lblQuote.userInteractionEnabled=TRUE;
        lblQuote.lineBreakMode=UILineBreakModeWordWrap;
        lblQuote.font=[UIFont systemFontOfSize:40.0];    
        lblQuote.tag=500;
        lblQuote.text=@"";




        CGSize maximumLabelSize = CGSizeMake(296,9999);

        CGSize expectedLabelSize = [strQuote sizeWithFont:lblQuote.font 
                                        constrainedToSize:maximumLabelSize 
                                            lineBreakMode:lblQuote.lineBreakMode]; 




        //adjust the label the the new height.
         int nooflines=expectedLabelSize.height/16.0;
        lblQuote.numberOfLines=nooflines;

        // CGRect newFrame = lblQuote.frame;
        //newFrame.size.height = expectedLabelSize.height;
        // yourLabel.frame = newFrame;

        lblQuote.textAlignment=UITextAlignmentCenter;


        UIPanGestureRecognizer *gesture = [[[UIPanGestureRecognizer alloc] 
                                            initWithTarget:self 
                                            action:@selector(labelDragged:)] autorelease];
        [lblQuote addGestureRecognizer:gesture];


- (void)labelDragged:(UIPanGestureRecognizer *)gesture
{
    UILabel *label = (UILabel *)gesture.view;
    CGPoint translation = [gesture translationInView:label];

    // move label
    label.center = CGPointMake(label.center.x + translation.x, 
                               label.center.y + translation.y);

    // reset translation


    [gesture setTranslation:CGPointZero inView:label];
}

我尝试在触摸事件上移动标签。代码如下:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
     UITouch *touch = [touches anyObject];
    CGPoint pointMoved = [touch locationInView:imgView2];  
    lblQuote.frame=CGRectMake(pointMoved.x, pointMoved.y, lblQuote.frame.size.width, lblQuote.frame.size.height);



}

但是移动不如平移手势识别器平滑。有时触摸是在另一个方向上,而在另一个方向上标记移动,有时或多或少的移动比触摸。

4

1 回答 1

4

好吧,这完全取决于您的逻辑,尽管我为您编写了代码,希望这对您有所帮助!确保您的 UILabel UserIntraction 设置为 YES;并使用触摸开始和触摸移动方法而不是pangesture ....现在看这个

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [[event allTouches]anyObject];
  if([touch view] == lblName)
  {
    NSLog(@"touch on label");
    CGPoint pt = [[touches anyObject] locationInView:lblName];
    startLocation = pt;
    // startLocation is a CGPoint declare globaly in .h..and lblName is your UILabel
  }
}

- (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event 
{
   UITouch *touch = [[event allTouches]anyObject];
   if([touch view] == lblName)
   {
     CGPoint pt = [[touches anyObject] previousLocationInView:lblName];
     CGFloat dx = pt.x - startLocation.x;
     CGFloat dy = pt.y - startLocation.y;
     CGPoint newCenter = CGPointMake(lblName.center.x + dx, lblName.center.y + dy);


    //now put if condition for dragging in specific area

    CGFloat min_X = lblName.center.x + dx - lblName.frame.size.width/2.0;
    CGFloat max_X = lblName.center.x + dx + lblName.frame.size.width/2.0;
    CGFloat min_Y = lblName.center.y + dy - lblName.frame.size.height/2.0;
    CGFloat max_Y = lblName.center.y + dy + lblName.frame.size.height/2.0;

    if((min_X >= imageView.frame.origin.x && max_X <=  imageView.frame.origin.x +  imageView.frame.size.width) && (min_Y >=  imageView.frame.origin.y && max_Y <=  imageView.frame.origin.y +  imageView.frame.size.height))
    {
        lblName.center = newCenter;
    }
  }
}
于 2012-06-07T07:09:06.427 回答