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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:28:14+00:00 2026-06-17T20:28:14+00:00

My goal was to create a button that moves itself to a random location

  • 0

My goal was to create a button that moves itself to a random location whenever you press it. I got this to work with this action:

- (IBAction)move:(id)sender 
{
   int x = 0 + arc4random() % (260 - 0);
   int y = 0 + arc4random() % (400 - 0);

   frame = self.button.frame;
   frame.origin.x = x; // new x coordinate
   frame.origin.y = y; // new y coordinate
   self.button.frame = frame;
}

However then I tried adding a timer, triggered by a button with:

- (IBAction)start:(id)sender 
{
   timer =[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
   self.startButton.Hidden = YES;
   self.label.hidden = NO; 
}

and

- (void)showActivity
{

   int currentTime = [self.label.text intValue];
   int newTime = currentTime - 1;
   self.label.text = [NSString stringWithFormat:@"%d", newTime];

   if (newTime == 0)
   {
       [timer invalidate];
   }
}

Every time the timer ticks, it seems to repaint the view. Before I start the timer, the button can be moved just fine. Then, once I press the second button which starts the timer, the first button is rooted where I originally placed it in my xib file. Is there any way I can fix this?

  • 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-17T20:28:15+00:00Added an answer on June 17, 2026 at 8:28 pm

    This problem is likely a result of autolayout (an iOS 6 feature that controls the placement of controls based upon arithmetic rules called constraints). To see if you have autolayout on, open your storyboard/NIB, press option+command–1 to go to the “file inspector” (or just click on the “file inspector” tab, the first one, on the rightmost panel) and see if “autolayout” is checked or not.

    If autolayout is on, even after changing frames, the constraints will be reapplied, and the control will be moved back to the location dictated by the constraints. You can either turn off autolayout, or leave autolayout on and either programmatically remove the constraints or programmatically change the constraints rather than changing the frame.

    See this answer for an example of how you might animate by changing constraints. But it’s easiest to just turn off autolayout:

    autolayout setting

    For background information and links to various sources on autolaout see the Cocoa Auto Layout Guide.

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

Sidebar

Related Questions

My goal is to create a button that when clicked deletes the parent divs
My end goal is to create a firefox extension that inserts an HTML button
My goal is to create a list from menu.bin. This is the func: pitem
My goal is to create an entry form (addnew.php) that will allow me to
My ultimate goal is to create a timer that produces a sound at an
My goal is to create an animation with the YUI Animation Utility that does
Hi here's the goal: to create an automatic bit of content that toggles open
I've created an object Chartblock that implements QGraphicsItem. My goal is to create a
My end goal is to create a checkbox that, when selected, checks all the
I'm trying to create an 'edit' button for a post that when clicked will

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.