Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict gesture to within the bounds of UIImageView?

Shalini named tag moving on imageview

I want to restrict black color image UIView to move within the girlimageview bounds.I should not move outside of girlimageview.

My girlimageview is static image with frame (5,0,310,320)

I am using UIGestureRecognizer to move the black image on imageview.

I tried with below code using UIPanGestureRecognizer to restrict but could not able to restrict it.

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

Any help would be appreciated.

like image 820
Vidhyanand Avatar asked Mar 21 '23 21:03

Vidhyanand


1 Answers

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

Here's the code. If black view is a subview of girl image view it should work.

like image 166
Roman Temchenko Avatar answered Mar 24 '23 10:03

Roman Temchenko