1

imageview に 1 つのラベルを追加し、ラベルをドラッグするための Pangesture を追加しました。どうすればこれを達成できますか? ラベルとジェスチャを追加するコードは次のとおりです。

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

touches event.code でラベルを移動しようとしましたが、以下のとおりです。

- (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 に設定されていることを確認してください。パンジェスチャの代わりにタッチビギンドとタッチムーブメソッドを使用します....これを今見てください

-(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 に答える