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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:47:01+00:00 2026-06-18T14:47:01+00:00

I am trying to create a control where a user can touch and move

  • 0

I am trying to create a control where a user can touch and move a button within a frame. Here is my code.

- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{

   UITouch *touch = [[event touchesForView:button] anyObject];

    // get delta
    CGPoint previousLocation = [touch previousLocationInView:button];
    CGPoint location = [touch locationInView:button];
    CGFloat delta_x = location.x - previousLocation.x;
    CGFloat delta_y = location.y - previousLocation.y;

    // move button
    button.center = CGPointMake(button.center.x + delta_x,
                                button.center.y + delta_y);   


}

I am able to move the button(by touching and dragging), but how to restrict the button, so that it can move only left/right within a rectangle frame.

  • 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-18T14:47:02+00:00Added an answer on June 18, 2026 at 2:47 pm

    Perhaps this method will help you. I used it in a simple pong game I made a while ago. It is for a UIView that is the bounce pad for the pong game. I have restricted the movement of the bounce pad to the x-direction and not outside of the screen bounds.

    If something ain’t clear write a comment and I’ll try to explain.

    // Method for movement of the bouncing pad. Restricted movement to x-axis inside of bounds.
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *aTouch = [touches anyObject];
        CGPoint loc = [aTouch locationInView:self];
        CGPoint prevloc = [aTouch previousLocationInView:self];
    
        CGRect myFrame = self.frame;
    
        // Checking how far we have moved from the previous location
        float deltaX = loc.x - prevloc.x;
    
        // Note that we only update the x-position of the pad to prevent it from moving in the y-direction.
        myFrame.origin.x += deltaX;
    
        // Making sure that the bouncePad cannot move outside of the screen
        if(myFrame.origin.x < 0){
            myFrame.origin.x = 0;
        } else if (myFrame.origin.x + myFrame.size.width > [UIScreen main Screen].bounds.size.width) {
            myFrame.origin.x = [UIScreen mainScreen].bounds.size.width - myFrame.size.width;
        }
    
        // Setting the bouncing pad frame to the one with the updated position from the touches moved event.
        [self setFrame:myFrame];
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create User Control which will work like a rich button. It's
I am trying to create a user control within a WPF application that will
Trying to create a MVC User Control in the Release Candidate and I can't
I'm trying to create a user control template that I can send as email.
I'm trying to create a user control which has a DataSource, which can accept
I'm trying to create a semi-transparent user control button whose background opacity changes from
Im trying to create a user control menu where links to a page's usercontrols
I'm trying to create a user control in WPF to represent a Go board
I'm trying to create an ascx control that can wrap content like a panel.
I am trying to create a custom wpf control, I'm wondering how I can

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.