如何更新View Controller中的元素

时间:2012-10-04 08:59:01

标签: iphone objective-c ios view controller

我对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];
}

对我做错了什么有帮助,我该怎么做才能实现我的目标?

谢谢!

2 个答案:

答案 0 :(得分:0)

绘制圆圈后,您必须更新视图。

[self.view setNeedsDisplay];

我认为那样做。

答案 1 :(得分:0)

您更新了circle而非self.view的属性,因此您需要在setNeedsDisplay上致电circle