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?

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
user2014741
  • 47
  • 1
  • 6
  • 1
    I'm rereading. Maybe I don't understand what you're trying to do. I get 'move a button when tapped'... what's the timer for? – danh Jan 27 '13 at 02:10

2 Answers2

0

How about this...

- (void)repositionViewRandomly:(UIView *)view {

    CGFloat width = view.bounds.size.width;
    CGFloat height = view.bounds.size.height;

    int x = arc4random() % (int)(view.superview.bounds.size.width - width);
    int y = arc4random() % (int)(view.superview.bounds.size.height - height);

    [UIView animateWithDuration:0.5 animations:^{
        view.frame = CGRectMake(x, y, width, height);
    }];
}

- (IBAction)buttonPressed:(id)sender {

    [self performSelector:@selector(repositionViewRandomly:) withObject:sender afterDelay:1.0];
}
danh
  • 62,181
  • 10
  • 95
  • 136
  • That certainly makes the button moving look a lot nicer! However, every time my timer ticks, it still moves my button back to its original position... – user2014741 Jan 27 '13 at 02:14
0

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.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044