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 8898211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:33:21+00:00 2026-06-15T00:33:21+00:00

What I need is when a UIImageView is dragged off of the screen it

  • 0

What I need is when a UIImageView is dragged off of the screen it to bounce back when it gets let go. I have it working in the left and top sides this is what I am doing.

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {          

        if (!CGRectContainsPoint(self.view.frame, imageView.frame.origin)){  

            CGFloat newX = 0.0f;
            CGFloat newY = 0.0f;

            // If off screen upper and left

            if (imageView.frame.origin.x < 0.0f){
                CGFloat negX = imageView.frame.origin.x * -1;
                newX = negX;
            }else{
                newX = imageView.frame.origin.x;
            }

            if (imageView.frame.origin.y < 0.0f){
                CGFloat negY = imageView.frame.origin.y * -1;
                newY = negY;
            }else{
                newY = imageView.frame.origin.y;
            }


            CGRect newPoint = CGRectMake(newX, newY, imageView.frame.size.width, imageView.frame.size.height);

            [UIView beginAnimations:@"BounceAnimations" context:nil];
            [UIView setAnimationDuration:.5];
            [letterOutOfBounds play];
            [imageView setFrame:newPoint];

            [UIView commitAnimations];

        }
    }
}

So I would like to achieve the same thing for the right and bottom sides. But I have been stuck at this for awhile. Any Ideas?

  • 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-15T00:33:22+00:00Added an answer on June 15, 2026 at 12:33 am

    How about something like the following?

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UIImageView *imageView = nil;
    
        BOOL moved = NO;
        CGRect newPoint = imageView.frame;
    
        // If off screen left
    
        if (newPoint.origin.x < 0.0f){
            newPoint.origin.x *= -1.0;
            moved = YES;
        }
    
        // if off screen up
    
        if (newPoint.origin.y < 0.0f){
            newPoint.origin.y *= -1.0;
            moved = YES;
        }
    
        // if off screen right
    
        CGFloat howFarOffRight = (newPoint.origin.x + newPoint.size.width) - imageView.superview.frame.size.width;
        if (howFarOffRight > 0.0)
        {
            newPoint.origin.x -= howFarOffRight * 2;
            moved = YES;
        }
    
        // if off screen bottom
    
        CGFloat howFarOffBottom = (newPoint.origin.y + newPoint.size.height) - imageView.superview.frame.size.height;
        if (howFarOffBottom > 0.0)
        {
            newPoint.origin.y -= howFarOffBottom * 2;
            moved = YES;
        }
    
        if (moved)
        {
            [UIView beginAnimations:@"BounceAnimations" context:nil];
            [UIView setAnimationDuration:.5];
            [letterOutOfBounds play];
            [imageView setFrame:newPoint];
    
            [UIView commitAnimations];
        }
    }
    

    As I read your code, the logic of “if off the left side, move it back on to the view by the same distance it was off the screen.” To be honest, that doesn’t quite make sense to me (why, when bouncing back, does the coordinate depend upon how far off the screen it was), but I’ve tried to honor that in the “off screen right” and “off screen bottom” logic. Obviously my logic is using the superview of imageView to determine the width of the containing view, but if that’s not appropriate, replace it with whatever is.

    Edit:

    I personally do this stuff with gesture recognizers, such as:

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.imageView addGestureRecognizer:pan];
    self.imageView.userInteractionEnabled = YES;
    

    Thus, a gesture recognizer to animate moving the image back would be:

    - (void)handlePan:(UIPanGestureRecognizer *)gesture
    {
        static CGRect originalFrame; // you could make this an ivar if you want, but just for demonstration purposes
    
        if (gesture.state == UIGestureRecognizerStateBegan)
        {
            originalFrame = self.imageView.frame;
        }
        else if (gesture.state == UIGestureRecognizerStateChanged)
        {
            CGPoint translate = [gesture translationInView:gesture.view];
    
            CGRect newFrame = originalFrame;
    
            newFrame.origin.x += translate.x;
            newFrame.origin.y += translate.y;
    
            gesture.view.frame = newFrame;
        }
        else if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled)
        {
            CGRect newFrame = gesture.view.frame;
    
            newFrame.origin.x = fmaxf(newFrame.origin.x, 0.0);
            newFrame.origin.x = fminf(newFrame.origin.x, gesture.view.superview.bounds.size.width - newFrame.size.width);
    
            newFrame.origin.y = fmaxf(newFrame.origin.y, 0.0);
            newFrame.origin.y = fminf(newFrame.origin.y, gesture.view.superview.bounds.size.height - newFrame.size.height);
    
            // animate how ever you want ... I generally just do animateWithDuration
    
            [UIView animateWithDuration:0.5 animations:^{
                gesture.view.frame = newFrame;
            }];
        }
    }
    

    Or, if you want a gesture recognizer that just prevents the dragging of the image off the screen in the first place, it would be:

    - (void)handlePan:(UIPanGestureRecognizer *)gesture
    {
        static CGRect originalFrame;
    
        if (gesture.state == UIGestureRecognizerStateBegan)
        {
            originalFrame = self.imageView.frame;
        }
        else if (gesture.state == UIGestureRecognizerStateChanged)
        {
            CGPoint translate = [gesture translationInView:gesture.view];
    
            CGRect newFrame = originalFrame;
    
            newFrame.origin.x += translate.x;
            newFrame.origin.x = fmaxf(newFrame.origin.x, 0.0);
            newFrame.origin.x = fminf(newFrame.origin.x, gesture.view.superview.bounds.size.width - newFrame.size.width);
    
            newFrame.origin.y += translate.y;
            newFrame.origin.y = fmaxf(newFrame.origin.y, 0.0);
            newFrame.origin.y = fminf(newFrame.origin.y, gesture.view.superview.bounds.size.height - newFrame.size.height);
    
            gesture.view.frame = newFrame;
        }
    }
    

    By the way, in iOS 7, you can give the animation of the image view back to its original location a little bounciness by using the new animationWithDuration with the usingSpringWithDampening and initialSpringVelocity parameters:

    [UIView animateWithDuration:1.0
                          delay:0.0
         usingSpringWithDamping:0.3
          initialSpringVelocity:0.1
                        options:0
                     animations:^{
                         // set the new `frame` (or update the constraint constant values that
                         // will dictate the `frame` and call `layoutViewsIfNeeded`)
                     }
                     completion:nil];
    

    Alternatively, in iOS7, you can also use UIKit Dynamics to add a UISnapBehavior:

    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    self.animator.delegate = self;
    
    UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.viewToAnimate snapToPoint:CGPointMake(self.viewToAnimate.center.x, self.view.frame.size.height - 50)];
    
    // optionally, you can control how much bouncing happens when it finishes, e.g., for a lot of bouncing:
    //
    // snap.damping = 0.2;
    
    // you can also slow down the snap by adding some resistance
    //
    // UIDynamicItemBehavior *resistance = [[UIDynamicItemBehavior alloc] initWithItems:@[self.viewToAnimate]];
    // resistance.resistance = 20.0;
    // resistance.angularResistance = 200.0;
    // [self.animator addBehavior:resistance];
    
    [self.animator addBehavior:snap];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIImageView that I need to animate off the screen. I'm doing
I have a UIImageView subclass and I need to have a pan gesture so
I have a subclass of UIImageView that I need to know when is touched.
I have an UIImageView and taking the raw touch input. I need to check
I have a UIScrollView which has an UIImageView in it. I need to be
Need some regular expressions help. So far I have my code working to allow
I need help with making a UIImageView float on the screen and when it
I have a UIImageView object in one of my classes, but I need to
I have a paginating scrollview with UIImageView's inside. At a point i need to
I have a UIimageView and I need to give some perspective to the image.

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.