I am having my UIImageView
onto which I am having another UIView
rectangle. By applying pan gesture to UIView
rectangle it gets outside of UIImageView
also. I don't want to be drag outside of UIImageView
I have tried below code but it is not working that way
-(void)handleMovementView:(UIPanGestureRecognizer *)recognizer
{
CGPoint movement;
if(recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged || recognizer.state == UIGestureRecognizerStateEnded)
{
CGRect rec = recognizer.view.frame;
CGRect imgvw = self.imgViewCrop.frame;
if((rec.origin.x >= imgvw.origin.x && (rec.origin.x + rec.size.width <= imgvw.origin.x + imgvw.size.width)))
{
CGPoint translation = [recognizer translationInView:recognizer.view.superview];
movement = translation;
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointZero inView:recognizer.view.superview];
[self handleMovementForHandlers:movement];
}
}
}
If i apply Pan slowly it applies this condition but when i go fast it went outside of ImageView
Instead of manually computing whether the points are within the view's bounds, use CGRectContainsPoint(rect, point)
. This is what works for me, and I like it because it's shorter and more readable:
func handlePan(pan: UIPanGestureRecognizer) {
switch pan.state {
case .Began:
if CGRectContainsPoint(self.pannableView.frame, pan.locationInView(self.pannableView)) {
// Gesture started inside the pannable view. Do your thing.
}
}
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