私の目標は、ボタンを押すたびにランダムな場所に移動するボタンを作成することでした。私はこれをこのアクションで動作させることができました:
- (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;
}
ただし、次のボタンでトリガーされるタイマーを追加してみました。
- (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;
}
と
- (void)showActivity
{
int currentTime = [self.label.text intValue];
int newTime = currentTime - 1;
self.label.text = [NSString stringWithFormat:@"%d", newTime];
if (newTime == 0)
{
[timer invalidate];
}
}
タイマーが作動するたびに、ビューが再描画されるようです。タイマーを開始する前に、ボタンをうまく動かすことができます。次に、タイマーを開始する2番目のボタンを押すと、最初のボタンは、xibファイルで最初に配置した場所にルートされます。これを修正する方法はありますか?