0
我想要一個按鈕隨機出現並讓用戶點擊它。但是,如果他們沒有在合適的時間內按下它,它會消失,我希望它移動到不同的位置。但是,每當我不點擊它時,它就會回到屏幕上的相同位置。這是我的代碼。按鈕不隨機移動
-(IBAction)randomRed:(id)sender
{
[self redDot];
CGRect senderFrame = [sender frame];
CGRect superBounds = [[sender superview ] bounds];
senderFrame.origin.x = (superBounds.size.width - senderFrame.size.width) * drand48();
senderFrame.origin.y = (superBounds.size.height - senderFrame.size.height) * drand48();
[sender setFrame:senderFrame];
counter = counter-1;
[self performSelector:@selector(showRedDot) withObject:nil afterDelay:1.0];
}
-(void)startRedDot
{
if (counter ==0)
redDot = [NSTimer scheduledTimerWithTimeInterval:4.0
target:self
selector:@selector(showRedDot)
userInfo:nil
repeats:YES];
}
-(void)showRedDot
{
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[redButton setAlpha:1];
[UIView commitAnimations];
[self performSelector:@selector(redDot) withObject:nil afterDelay:1.0];
}
}
-(void)redDot
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[redButton setAlpha:0];
[UIView commitAnimations];
}