1

imageview에 하나의 라벨을 추가하고 드래그 label.Put에 대한 Pangesture를 추가했습니다.이 코드 라벨은 전체적으로 드래그됩니다.하지만 uiimageview 프레임 내에서 라벨 드래그를 제한하고 싶습니다. 어떻게해야합니까? lable과 제스처를 추가하는 코드는 다음과 같습니다.uilabel이 특정 프레임 내에서만 드래그되도록 제한하는 방법은 무엇입니까?

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; 
     [email protected]""; 




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

나는 다음과 같이 터치의 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); 



} 

을하지만 팬 제스처는 터치 recognizer.sometimes로의 이동이 원활하지 것은 다른 또 다른 방향 및 레이블 운동에 방향 및 때로는 터치보다 움직임이 많거나 적습니다.

+0

UIPanGestureRecognizer 대신에 touches 메서드를 사용하는 것이 좋습니다. 프레임을 계속 확인하고 사용자를 제한 할 수 있습니다. – rishi

답변

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

감사합니다!

+0

@Bhoomi - 당신을 위해 일하는 것인가,하지 않습니까 ??? – TheTiger

+0

나를 위해 일했습니다. 고마워요! –

+0

그것은 나에게도 효과가있다. 감사! –