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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:22:54+00:00 2026-06-04T22:22:54+00:00

I am doing some drag and rotation calculations using UIPanGestureRecognizer. The rotation angle is

  • 0

I am doing some drag and rotation calculations using UIPanGestureRecognizer. The rotation angle is correct, and the drag location is almost correct. The problem is that as you go around the center of the box needs to be adjusted according to the angle and I can’t figure out how.

I’ve included pictures of what a 180 rotation looks like but where the finger is during the rotation. I just don’t know how to adjust to make the block stay with your finger appropriately. And heres a video just to clarify because it is strange behavior. http://tinypic.com/r/mhx6a1/5

EDIT: Here is a real world video of what should be happening. The problem being that in the iPad video your finger is moving where in the real world your finger would be cemented in a particular place on the item moving. The math needed is to adjust your touch location along the angle with a difference from the actual center. I just can’t figure out the math. http://tinypic.com/r/4vptnk/5

first shot

second shot

third shot

Thanks very much!

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // set original center so we know where to put it back if we have to.
        originalCenter = dragView.center;

    } else if (gesture.state == UIGestureRecognizerStateChanged) {
        [dragView setCenter:CGPointMake( originalCenter.x + [gesture translationInView:self.view].x , originalCenter.y + [gesture translationInView:self.view].y )];

        CGPoint p1 = button.center;
        CGPoint p2 = dragView.center;

        float adjacent = p2.x-p1.x;
        float opposite = p2.y-p1.y;

        float angle = atan2f(adjacent, opposite); 

        [dragView setTransform:CGAffineTransformMakeRotation(angle*-1)];

    }
}
  • 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-04T22:22:56+00:00Added an answer on June 4, 2026 at 10:22 pm

    I’ve finally solved this issue and have it working perfectly. Persistence am I right??

    Here is the code for the solution with a few comments to explain the changes.

    - (void)handlePan:(UIPanGestureRecognizer *)gesture
    {
        if (gesture.state == UIGestureRecognizerStateBegan) {
            // Get the location of the touch in the view we're dragging.
            CGPoint location = [gesture locationInView:dragView];
    
            // Now to fix the rotation we set a new anchor point to where our finger touched. Remember AnchorPoints are 0.0 - 1.0 so we need to convert from points to that by dividing
            [dragView.layer setAnchorPoint:CGPointMake(location.x/dragView.frame.size.width, location.y/dragView.frame.size.height)];
    
    
        } else if (gesture.state == UIGestureRecognizerStateChanged) {
            // Calculate Our New Angle
            CGPoint p1 = button.center;
            CGPoint p2 = dragView.center;
    
            float adjacent = p2.x-p1.x;
            float opposite = p2.y-p1.y;
    
            float angle = atan2f(adjacent, opposite); 
    
            // Get the location of our touch, this time in the context of the superview.
            CGPoint location = [gesture locationInView:self.view];
    
            // Set the center to that exact point, We don't need complicated original point translations anymore because we have changed the anchor point.
            [dragView setCenter:CGPointMake(location.x, location.y)];
    
            // Rotate our view by the calculated angle around our new anchor point.
            [dragView setTransform:CGAffineTransformMakeRotation(angle*-1)];
    
        }
    }
    

    Hope my month+ struggle and solution helps someone else in the future. Happy Coding 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While doing some refactoring I've found that I'm quite often using a pair or
Been doing some playing call my service which is on a different domain using
While doing some small regex task I came upon this problem. I have a
Trying an example piece of code for Scriptaculous for doing some drag and drop.
I have a Winforms app that allows the user to drag and drop some
I'm using the SilverLight Toolkit to implement some drag/drop functionality in a Silverlight 4
I'm doing some Drag and Drop operations, and I want to use a visual
We've got some pretty complex code that's utilising the drag and drop functionality provided
Doing some jquery animation. I have certain divs set up with an attribute of
Doing some homework here (second assignment, still extremely green...). The object is to read

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.