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.
- (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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With