如何将手势限制在UIImageView的范围内?

时间:2013-10-28 09:43:36

标签: ios objective-c uiimageview uigesturerecognizer uipangesturerecognizer

Shalini named tag moving on imageview

我想限制黑色图像UIView在girlimageview bounds内移动。我不应该移动到girlimageview之外。

我的girlimageview是带框架的静态图像(5,0,310,320)

我正在使用UIGestureRecognizer在imageview上移动黑色图片。

我尝试使用UIPanGestureRecognizer使用以下代码进行限制但无法限制它。

UIPanGestureRecognizer *panTagGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[panTagGesture setDelegate:self];
[blackanimateview addGestureRecognizer:panTagGesture];


 -(void) handlePan:(UIGestureRecognizer*)panGes{

        CGPoint point = [panGes locationInView:girlimageview];

        if (point.x < girlimageview.bounds.size.width) {

            CGRect newframe = CGRectMake(point.x, point.y, blackanimateview.frame.size.width, blackanimateview.frame.size.height);

            blackanimateview.frame = newframe;

        }
        if (point.y < girlimageview.bounds.size.height-160) {

            CGRect newframe = CGRectMake(point.x, point.y, blackanimateview.frame.size.width, blackanimateview.frame.size.height);

            blackanimateview.frame = newframe;  
        }
    }

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:6)

- (void)handlePan:(UIGestureRecognizer*)panGes {
CGPoint point = [panGes locationInView:girlimageview];
CGRect girlImageViewBounds = girlimageview.bounds;
CGRect blackAnimateViewFrame = blackanimateview.frame;
CGFloat newX = MIN(point.x, girlImageViewBounds.size.width - blackAnimateViewFrame.size.width);
newX = MAX(newX, 0.0f);
CGFloat newY = MIN(point.y, girlImageViewBounds.size.height - blackAnimateViewFrame.size.height);
newY = MAX(newY, 0.0f);
blackAnimateViewFrame.origin.x = newX;
blackAnimateViewFrame.origin.y = newY;
blackanimateview.frame = blackAnimateViewFrame;
}

这是代码。如果黑色视图是女孩图像视图的子视图,它应该可以工作。