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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:25:02+00:00 2026-06-03T03:25:02+00:00

How can I constrain the drag-path of a button so that it can only

  • 0

How can I constrain the drag-path of a button so that it can only be dragged to the centre of the screen from any point I want?

I know the current position of the UIButton and the centre coordinates.
I need the current x and y formula for the drag event.

  • 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-03T03:25:04+00:00Added an answer on June 3, 2026 at 3:25 am

    This should do it. Save the start position of the drag, get the current position on each move event. Pass those to this function. It will answer a vector which can be applied to the button’s frame.

    The gist of the calculation is to determine a magnitude by choosing the smallest component of the drag (x or y) and answering a vector with the same magnitude in both dimensions (preserving the signs over both axes).

    // answer a vector to apply to an object's frame, constrained to a diagonal
    - (CGPoint)constrainToDiagonalFrom:(CGPoint)from to:(CGPoint)to {
    
        CGPoint diff = CGPointMake(to.x-from.x, to.y-from.y);
        CGFloat magnitude = MIN(fabs(diff.x), fabs(diff.y));    // this is how large the drag will be
        return CGPointMake(copysignf(magnitude, diff.x), copysignf(magnitude, diff.y));
    }
    

    Call it like this:

    // on touches moved
    // we saved startPoint on touches began
    // get location from this event's touches
    
    CGPoint diagonal = [self constrainToDiagonalFrom:startPoint to:location];
    myButton.frame = CGRectOffset(myButton.frame, diagonal.x, diagonal.y);
    

    You can fool around with the magnitude calculation.. largest component, smallest, average, etc. As long as the result is symmetrical in both dimensions.

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

Sidebar

Related Questions

How do you constrain the elements of a PanelPositioned so they can only be
How can I match a pattern in regex that can contain anything (letters,numbers,...) but
My DB table has a column that can contain a very big amount of
So we have a database column that can contain just about anything. Unicode, numbers,
I need to implement a drag and drop functionality, where I can define and
When dragging an object, the user can drag in the x and y axes.
How can I constrain a vertical WPF StackPanel 's width to the most narrow
How can I restart a foreach loop in C#?? For example: Action a; foreach(Constrain
I want to make the drag drop to the element,and I dot not want
I'm looking for a control for C# that can contain other controls, takes care

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.