2012-10-04 53 views
3

我對Objective-C開發相當陌生。 在我從頭開始構建的第一個應用程序中,我想繪製一個隨時間推移而增大其大小的圓。 問題是我已經能夠通過屏幕移動圓圈,但似乎無法更新其屬性,因此它變得更大或更小。如何更新視圖控制器中的元素

事情是這樣的:

CCircle class 

@interface CCircle : CShapes 

@property float radius; 
@property float startAngleRadians; 
@property float endAngleRadians; 

在第一視圖控制器(ShapesViewController.m):

- (void) viewDidLoad 
{ 
    (...) 
    CCircle* circle = [[CCircle alloc] initWithFrame:frame]; 
    circle.radius = 40; 
    circle.startAngleRadians = M_PI; 
    circle.endAngleRadians = 2*M_PI; 
    circle.tag = 1; 
    [self.view addSubView:circle]; 

    //I also schedule an update method, so that it is called every 1 seconds. 
    [NSTimer scheduledTimerWithTmieInterval:1.0 target:self selector:@selector(updateCircle) userInfo:nil repeats:YES]; 
} 

同樣在ShapesViewController.m,更新方法:

- (void) updateCircle 
{ 
    CCircle *circle = [self.view viewWithTag:1]; 

    //Now here: if I do this: the circle will "move" through the screen 
    CGRect frame = circle.frame; 
    frame.origin.x += 5; 
    [circle setFrame:frame]; 

    //However, if I try to change the circle properties, I don't know what to do so 
    //that affects the circle. In this case, the circle will move through the screen, 
    //but I keep seeing always the same size(radius). 
    circle.radius += 5; 

    //I've tried the following (of course, not all at the same time): 
    //[self.vew addSubview:circle]; 
    //[self.view sendSubviewToBack:circle]; 
    //[self.view sendSubviewToFront:circle]; 
    //[self.view setNeedsDisplay]; 
    //[self.view setNeedsLayout]; 
} 

任何幫助我做錯了什麼,我該怎麼做才能達到我想要的?

謝謝!

+0

您更新了'circle'屬性,而不是'self.view',所以我認爲在'circle'上調用'setNeedsDisplay'值得一試。 –

+0

就是這樣,感謝一噸! – FerranMG

+0

很高興工作!我補充說,作爲幫助解決問題的正式答案。 –

回答

0

你更新的circle,不self.view的屬性,所以你需要調用setNeedsDisplaycircle

0

繪製圓後,您必須更新您的視圖。

[self.view setNeedsDisplay]; 

我認爲這樣做。

+0

我已經試過了(這實際上是我嘗試的第一件事),並且不起作用。 – FerranMG

相關問題