4

Shalini 命名标签在 imageview 上移动

我想限制黑色图像 UIView 在girlimageview 范围内移动。我不应该移动到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;  
        }
    }

任何帮助,将不胜感激。

4

1 回答 1

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

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

于 2013-10-28T09:59:42.037 回答