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?