2010-05-24 64 views
0

這是我的設置。我有一個viewcontroller,我創建並添加爲子視圖。 viewcontroller提供了一些用戶可以選擇的選項。響應「長按」手勢正在推動視控制器。在視圖控制器中,我添加了一個子UIView來將其他控件分組在一起,這樣我就可以將它們作爲一個單元在屏幕上移動,並且當它們顯示時,將它們置於長按的位置。下面是實例化視圖控制器,改變它的位置代碼,並將它作爲一個子視圖:動態更改UIView的位置

UserOptions *opts = [[UserOptions alloc] initWithNibName:@"UserOptions" bundle:nil]; 
[opts recenterOptions:location]; 
[self.view addSubview:opts.view]; 

的代碼並創建和推動的ViewController,但調用recenterOptions不會做任何位。這裏是該方法:

- (void) recenterOptions:(CGPoint)location { 
    CGRect oldFrame = self.optionsView.frame; 

    CGFloat newX = location.x; // + oldFrame.size.width/2.0; 
    CGFloat newY = location.y; // + oldFrame.size.height/2.0; 

    CGRect newFrame = CGRectMake(newX, newY, oldFrame.size.width, oldFrame.size.height); 

    self.optionsView.frame = newFrame; 
} 

請注意,self.optionsView是我添加到viewcontroller的筆尖的子UIView。

有誰知道爲什麼我無法更改UIView的位置?

問候, 埃裏克

+1

此問題的標題與問題本身無關。您可能需要更新它以獲得更好的響應。 – iwasrobbed 2010-05-24 19:54:30

回答

0

一對夫婦的事情。首先,嘗試調用-recenterOptions之前添加視圖視圖層次:

UserOptions *opts = [[UserOptions alloc] initWithNibName:@"UserOptions" 
                bundle:nil]; 
[self.view addSubview:opts.view]; 
[opts recenterOptions:location]; 

接下來,剛剛成立的,而不是試圖改變它的框架視圖的中心:

- (void) recenterOptions:(CGPoint)location { 
    [[self optionsView] setCenter:location]; 
} 

你驗證你的optionsView有效(非零)?

我同意羅布,順便說一句。您需要更改您的標題以符合您的問題。

+0

這樣做。反轉這兩個調用使代碼正常工作。 我會更新標題。不知道我第一次做錯了。 – EricM 2010-05-26 22:02:13

0

視圖加載懶惰。在您致電self.view之前,視圖未加載,optionsView爲零,因此self.optionsView.frame = newFrame什麼都不做。