仅在UIImageview框架中使用UIPanGestureRecognizer拖动UILable

时间:2015-08-04 05:22:51

标签: uiview uiimageview uilabel uigesturerecognizer uipangesturerecognizer

UILable 拖动 UIView 框架不在 UIImageView 框架中我使用以下代码使用 UIPanGestureRecognizer 拖动标签

 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture1:)];
    [panGesture setMinimumNumberOfTouches:1];
    [self.imageView setBounds:CGRectMake(self.imageView.frame.origin.x,self.imageView.frame.origin.y ,self.imageView.frame.size.width, self.imageView.frame.size.height)];
    [panGesture setMaximumNumberOfTouches:1];
    panGesture.delegate=self;
    [title addGestureRecognizer:panGesture];
    panGesture = nil;

我的行动方法

-(void)handlePanGesture1:(UIPanGestureRecognizer *)sender
{

  CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.imageView];
  if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) 

{

     firstX = [[sender view] center].x;
     firstY = [[sender view] center].y;
}

 translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

    [[sender view] setCenter:translatedPoint];
}

1 个答案:

答案 0 :(得分:2)

好吧,你应该用这样的东西限制你的x,y:

    CGFloat maxPossibleX = CGRectGetMaxX(self.imageView.frame) - (CGRectGetWidth(sender.view.bounds)/2.0f);
    CGFloat maxPossibleY = CGRectGetMaxY(self.imageView.frame) - (CGRectGetHeight(sender.view.bounds) /2.0f);

    CGFloat minPossibleX = CGRectGetMinX(self.imageView.frame) + CGRectGetMidX(sender.view.bounds);
    CGFloat minPossibleY = CGRectGetMinY(self.imageView.frame) + CGRectGetMidY(sender.view.bounds);


    CGFloat actualX = translatedPoint.x;
    actualX= MAX(minPossibleX, actualX);
    actualX = MIN(maxPossibleX, actualX);

    CGFloat actualY = translatedPoint.y;
    actualY= MAX(minPossibleY, actualY);
    actualY = MIN(maxPossibleY, actualY);

    translatedPoint = CGPointMake(actualX, actualY);

我假设你的标签不是你在这里的imageView的子视图。如果您的标签是imageView的子视图:

    CGFloat maxPossibleX = CGRectGetMaxX(self.imageView.bounds) - (CGRectGetWidth(sender.view.bounds)/2.0f);
    CGFloat maxPossibleY = CGRectGetMaxY(self.imageView.bounds) - (CGRectGetHeight(sender.view.bounds) /2.0f);

    CGFloat minPossibleX = CGRectGetMidX(sender.view.bounds);
    CGFloat minPossibleY = CGRectGetMidY(sender.view.bounds);


    CGFloat actualX = translatedPoint.x;
    actualX= MAX(minPossibleX, actualX);
    actualX = MIN(maxPossibleX, actualX);

    CGFloat actualY = translatedPoint.y;
    actualY= MAX(minPossibleY, actualY);
    actualY = MIN(maxPossibleY, actualY);

    translatedPoint = CGPointMake(actualX, actualY);

编辑:我已在测试项目中成功测试了此方法:

-(void)handlePanGesture1:(UIPanGestureRecognizer *)sender
{

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.imageView];
    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)

    {

        firstX = [[sender view] center].x;
        firstY = [[sender view] center].y;
    }

    translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

    CGFloat maxPossibleX = CGRectGetMaxX(self.imageView.bounds) - (CGRectGetWidth(sender.view.bounds)/2.0f);
    CGFloat maxPossibleY = CGRectGetMaxY(self.imageView.bounds) - (CGRectGetHeight(sender.view.bounds) /2.0f);

    CGFloat minPossibleX = CGRectGetMidX(sender.view.bounds);
    CGFloat minPossibleY = CGRectGetMidY(sender.view.bounds);


    CGFloat actualX = translatedPoint.x;
    actualX= MAX(minPossibleX, actualX);
    actualX = MIN(maxPossibleX, actualX);

    CGFloat actualY = translatedPoint.y;
    actualY= MAX(minPossibleY, actualY);
    actualY = MIN(maxPossibleY, actualY);

    translatedPoint = CGPointMake(actualX, actualY);
    [[sender view] setCenter:translatedPoint];
}