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

  • Home
  • SEARCH
  • 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 8816767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:41:37+00:00 2026-06-14T04:41:37+00:00

I have a UIPanGestureRecognizer that is attached to a UIView. What I am trying

  • 0

I have a UIPanGestureRecognizer that is attached to a UIView. What I am trying to do is essentially drag and drop into a bucket. If the user lets go of the UIView and it’s not in the bucket, I want it to animate back to its original start location. My selector that gets called for my UIPanGestureRecognizer is:

- (void)handleDragDescriptionView:(UIPanGestureRecognizer *)gestureRecognizer {
    UIView *panViewPiece = [gestureRecognizer view];
    CGRect originalFrame = CGRectMake(946, 20, 58, 30);

    CGPoint translation = [gestureRecognizer translationInView:panViewPiece];
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan || gestureRecognizer.state == UIGestureRecognizerStateChanged) {
        panViewPiece.center = CGPointMake(panViewPiece.center.x + translation.x, panViewPiece.center.y + translation.y);

        [gestureRecognizer setTranslation:CGPointZero inView:panViewPiece.superview];
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
        [UIView animateWithDuration:0.25 animations:^{
            self.dragDescriptionView.frame = originalFrame;
        }];
    }
    else {
        [UIView animateWithDuration:0.25 animations:^{
            self.dragDescriptionView.frame = originalFrame;
        }];
    }

}

I was wondering if I could do this without the originalFrame at the top of the method. I originally had

CGRect originalFrame = _dragDescriptionView.frame; 

instead of the hardcoding, but it doesn’t snap back. Probably because I’m updating that value as I am dragging. I don’t particularly like hardcoding values, but I wasn’t sure if there was a way around this. Thanks!

  • 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-14T04:41:39+00:00Added an answer on June 14, 2026 at 4:41 am

    You really only need to track the view’s original center. Make originalCenter an instance variable and only set it when the gesture begins:

    @implementation MyViewController {
        CGPoint _originalCenter;
    }
    
    - (void)handleDragDescriptionView:(UIPanGestureRecognizer *)gestureRecognizer {
        UIView *panViewPiece = [gestureRecognizer view];
    
        switch (gestureRecognizer.state) {
            case UIGestureRecognizerStateBegan:
                _originalCenter = panViewPiece.center;
                break;
    
            case UIGestureRecognizerStateChanged: {
                CGPoint translation = [gestureRecognizer translationInView:panViewPiece.superview];
                panViewPiece.center = CGPointMake(panViewPiece.center.x + translation.x, panViewPiece.center.y + translation.y);
                [gestureRecognizer setTranslation:CGPointZero inView:panViewPiece.superview];
                break;
            }
    
            case UIGestureRecognizerStateEnded:
            case UIGestureRecognizerStateCancelled: {
                [UIView animateWithDuration:0.25 animations:^{
                    panViewPiece.center = _originalCenter;
                }];
                break;
            }
    
            default:
                break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have UIView in which I want to be able to drag and drop
I want to trigger a UIPanGestureRecognizer attached to a UIView subclass when the initial
In my app, I have a map that allows the user to drop pins
I have a UIView with a UIPanGestureRecognizer attached to it. I also have an
I have a UIView object that can be dragged around using UIPanGestureRecognizer, but I
I have added a gesture recognizer (two finger tap) that detects when the user
(edited for clarity) I have a UITableView. On top of that is a UIView
On one of the UIViewControllers of my iPhone app, I have a UIPanGestureRecognizer attached
I have a main view that divided into 4 equally sized sub-views. I used
I have an application that allows a user to pan and zoom in on

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.