【问题标题】:How to update elements in a View Controller如何更新视图控制器中的元素
【发布时间】:2012-09-25 06:44:27
【问题描述】:

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

关于我做错了什么以及我应该怎么做才能实现我想要的任何帮助?

谢谢!

【问题讨论】:

  • 您更新了circle 的属性,而不是self.view,所以我认为在circle 上调用setNeedsDisplay 值得一试。
  • 很高兴它成功了!我将其添加为帮助解决问题的正式答案。

标签: iphone objective-c ios view controller


【解决方案1】:

您更新了circle 的属性,而不是self.view,因此您需要在circle 上调用setNeedsDisplay

【讨论】:

    【解决方案2】:

    画完圆圈后,你必须更新你的视图。

    [self.view setNeedsDisplay];
    

    我想这样就可以了。

    【讨论】:

    • 我已经试过了(这实际上是我尝试的第一件事),但不起作用。
    猜你喜欢
    • 2017-04-26
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    相关资源
    最近更新 更多