Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7952861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:59:20+00:00 2026-06-04T02:59:20+00:00

I have a small imageView that moves on the screen using the UIPanGestureRecognizer ,

  • 0

I have a small imageView that moves on the screen using the UIPanGestureRecognizer, but when it comes out from the edges of the superview does not get more touches and therefore can no longer turn back. How can I prevent that the image comes out from the edges of the superview?
This is the code:

- (void)pan:(UIPanGestureRecognizer *)gestureRecognizer
{
if (inLongPress) {
    UIView *hitView = [gestureRecognizer view];

    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

    if (!(CGRectContainsRect(self.imageView.frame, hitView.frame))) {

        [UIView beginAnimations:@"" context:NULL];
        [hitView setCenter:CGPointMake(_prevCenter.x, _prevCenter.y)];
        [UIView commitAnimations];

     }
    _prevCenter=hitView.center;

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer   state] == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gestureRecognizer translationInView:[hitView superview]];

        [hitView setCenter:CGPointMake([hitView center].x + translation.x, [hitView center].y + translation.y)];

        [gestureRecognizer setTranslation:CGPointZero inView:[hitView superview]];
    }

    if ([gestureRecognizer state] == UIGestureRecognizerStateEnded){
        inLongPress=NO;
        hitView.alpha=1.0;
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:59:21+00:00Added an answer on June 4, 2026 at 2:59 am

    just use a couple of if that limit the CGPoint to an allowed range.

    Something like this makes sure that you can’t move the object outside of the view:

    CGFloat proposedX = location.x;
    CGFloat proposedY = location.y;
    
    CGRect objectFrame = hitView.frame;
    CGFloat allowedXMin = 0 + objectFrame.size.width / 2.0f;
    CGFloat allowedXMax = self.view.bounds.size.width - objectFrame.size.width / 2.0f;
    CGFloat allowedYMin = 0 + objectFrame.size.height / 2.0f;
    CGFloat allowedYMax = self.view.bounds.size.height - objectFrame.size.height / 2.0f;
    
    if (proposedX < allowedXMin) {
        proposedX = allowedXMin;
    }
    else if (proposedX > allowedXMax) {
        proposedX = allowedXMax;
    }
    
    if (proposedY < allowedYMin) {
        proposedY = allowedYMin;
    }
    else if (proposedY > allowedYMax) {
        proposedY = allowedYMax;
    }
    location = CGPointMake(proposedX, proposedY);
    
    hitView.center = location;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an app, in which I have two small imageview (using have
I have a rather small but weird problem that I can't seem to fix.
If i have an ImageView that fills the screen. The ImageView background is set
I have this image that comes back from an API, which represents the users
I have small database of business and their addresses. Using the Google Geocode API
I have small web app that generate PDF files as a report. I'm trying
i have small function that running on condintion, if text box value is not
I have small game engine written in c++. I'm considering using some features of
I have a very small activity that must show an image. If picture is
I' have a view that contains several textViews an ImageView and a Button .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.