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로의 이동이 원활하지 것은 다른 또 다른 방향 및 레이블 운동에 방향 및 때로는 터치보다 움직임이 많거나 적습니다.
UIPanGestureRecognizer 대신에 touches 메서드를 사용하는 것이 좋습니다. 프레임을 계속 확인하고 사용자를 제한 할 수 있습니다. – rishi