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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:31:21+00:00 2026-06-10T12:31:21+00:00

Hi all you smart people out there! I want to create a touch interface

  • 0

Hi all you smart people out there!
I want to create a touch interface to an iOS app, that allows the user to drag an object on the screen around.
However, this object should be restricted to move along the perimeter of a circle, so that if the user is trying to drag the object outside that path, it would stick to the nearest point of that circle.
I have done some iPhone programming, but my math is poor. Please help!

  • 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-10T12:31:22+00:00Added an answer on June 10, 2026 at 12:31 pm

    All you have to do is set the frame of the view to follow the equation of a circle (of the form: (x-a)^2 + (y-b)^2 = r^2). Once you detect the touch point, you can restrict the view’s frame according to the x or the y coordinate of the touch point (both ways are the same).

    #define circleRadius 60.0 // r in the above eqtn
    #define circlesCenter_X 160.0 // a in the above eqtn
    #define circlesCenter_Y 200.0 // b in the above eqtn
    #define circleCenter_y(x) sqrtf(circleRadius*circleRadius - (x-circlesCenter_X)*(x-circlesCenter_X))
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch *touch = [[event touchesForView:firstPieceView] anyObject];
        CGPoint previousLocation = [touch previousLocationInView:self.view];
        CGPoint location = [touch locationInView:self.view];
        CGFloat delta_x = previousLocation.x - location.x; // constrained by x in this eg.
        CGFloat newX = firstPieceView.center.x-delta_x;
        // do limit the view's x-coordinate for possible solutions
        if(newX<circlesCenter_X - circleRadius)
            newX = circlesCenter_X - circleRadius;
        if(newX>circlesCenter_X + circleRadius)
            newX = circlesCenter_X + circleRadius;
        firstPieceView.center = CGPointMake(newX, circleCenter_y(newX)*(location.y>=circlesCenter_Y?1:-1) + circlesCenter_Y);
    }
    

    EDIT- Better solution:

    #define circleRadius 60.0 // r in the above eqtn
    #define circlesCenter_X 160.0 // a in the above eqtn
    #define circlesCenter_Y 200.0 // b in the above eqtn
    #define slope(x,y) (y-circlesCenter_Y)/(x-circlesCenter_X)
    #define pointOnCircle_X(m) circleRadius/(sqrtf(m*m + 1))
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch *touch = [[event touchesForView:self.view] anyObject];
        CGPoint location = [touch locationInView:self.view];
        CGFloat slope;
        CGPoint pointOnCircle;
        if(location.x==circlesCenter_X){// case for infinite slope
            pointOnCircle.x = circlesCenter_X;
            if(location.x<circlesCenter_X){
                pointOnCircle.y = circlesCenter_Y - circleRadius;
            }else{
                pointOnCircle.y = circlesCenter_Y + circleRadius;
            }
        }else{
            slope = slope(location.x,location.y);
            if(location.x<circlesCenter_X){
                pointOnCircle.x = circlesCenter_X - pointOnCircle_X(slope);
            }else{
                pointOnCircle.x = circlesCenter_X + pointOnCircle_X(slope);
            }
            pointOnCircle.y = slope * (pointOnCircle.x - circlesCenter_X) + circlesCenter_Y;
        }
        firstPieceView.center = pointOnCircle;
    }
    

    This can be applied similarly for Android, Blackberry, etc too!

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

Sidebar

Related Questions

I have a table that lists people and all their contact info. I want
There are a lot of sites out there that teach people how to build
In my desktop program, I want to check that to see if the user
I hope this is an interesting question for some people. I want to create
What are best practices to include boost smart pointer library only without adding all
I was wondering if using Lightbox all over the site would be a smart
My table is like this on Postgres, note that all days start by 01,
I'm trying to retrieve all columns that start with any non alpha characters in
I created this regex to match all words that start with @ in my
I've been trying to figure out what computer field I want to go into

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.