步驟1:將這個代碼在你ViewDidLoad
方法,其中,我已經創造了一些UIImageView
並將其添加到隨機查看
[self.view setTag:1];
for(int i=0;i<4;i++)
{
int x = arc4random()%300;
int y = arc4random()%400;
#warning set Image here
UIImageView *imgview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage.png"]];
[imgview setFrame:CGRectMake(x, y, 25, 25)];
[imgview setUserInteractionEnabled:YES];
[self.view addSubview:imgview];
}
第2步:定義touchBegan
方法來處理觸摸和移動視圖周圍的對象,我們已設置標記= 1爲ViewController
,因爲我們不想移動我們的主視圖,只有子視圖將被移動
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if([[touch view] tag] != 1)
{
[UIView animateWithDuration:0.25f animations:^{
int x = arc4random()%300;
int y = arc4random()%400;
[[touch view] setCenter:CGPointMake(x, y)];
}];
}
}
你有什麼試圖改變上述項目以適應你的需求? – 2013-04-25 08:35:02
你應該看看IBOutlets如何工作,以及如何響應觸摸輸入來移動一個對象,你只需要改變它的框架(或在超級視圖中的位置),如果你想讓動畫考慮一個動畫塊。 你可以在互聯網上找到關於這些主題的一切。 – Maurice 2013-04-25 08:43:59