使用arc4random()生成随机数,您可以生成x和y坐标以将按钮移动到.您想要考虑按钮的宽度和高度,以便它不会部分偏离屏幕,也不会影响屏幕的宽度和高度,因此它不会完全脱离屏幕.
-(void)buttonPressed:(id)sender {
UIButton *button = (UIButton *)sender;
int xmin = ([button frame].size.width)/2;
int ymin = ([button frame].size.height)/2;
int x = xmin + (arc4random() % (view.frame.size.width - button.frame.size.width));
int y = ymin + (arc4random() % (view.frame.size.height - button.frame.size.height));
[button setCenter:CGPointMake(x, y)];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7612 次 |
| 最近记录: |