首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >setNeedsDisplay不解雇DrawRect

setNeedsDisplay不解雇DrawRect
EN

Stack Overflow用户
提问于 2013-05-01 03:04:22
回答 1查看 9K关注 0票数 7

我有一个viewController,它显然是UIViewController的一个子类,名为MapViewController。在这个viewController中,我使用GPS来获取用户的位置。

我还有一个叫做DrawCircle的视图。此视图是UIView的子类。

使用drawCircle,我希望能够在任何时候使用我的MapViewController。但我不知道我是否明白这样做的概念。我知道我的绘图代码是有用的,我以前用过。但我不知道如何使用MapViewController使用DrawCircle

在我看来,无论何时我调用[myCustomView setNeedsDisplay],它都不是在我看来调用DrawRect方法。

下面是一些代码:MapViewController.h

代码语言:javascript
复制
#import "DrawCircle.h"


@interface MapViewController: UIViewController <CLLocationManagerDelegate>{
    DrawCircle *circleView;
}

@property (nonatomic, retain) DrawCircle *circleView;
@end

MapViewController.m

代码语言:javascript
复制
#import "DrawCircle.h"

@interface MapViewController ()
@end

@implementation MapViewController
@synthesize circleView;

- (void) viewDidLoad
{
    circleView = [[DrawCircle alloc] init];
    [self setNeedsDisplay];

    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

DrawCircle.m

代码语言:javascript
复制
#import "DrawCircle.h"

@interface DrawCircle() 
@end

@implementation DrawCircle

-(id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{

    CGContextRef ctx = UIGraphicsGetCurrentContext();


      CGPoint point = CGPointMake(40, 137);
      CGContextAddEllipseInRect(ctx, CGRectMake(point.x, point.y, 10, 10));
    }

    CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor redColor] CGColor]));
    CGContextFillPath(ctx);
}

此外,如果这对我的思维过程提供了任何帮助,这里是我的StoryBoard场景。

其中视图控制器自定义类为MapViewController,视图自定义类为DrawCircle。

**编辑:**我还想提到,在DrawCircle.m中,我有从MapViewController.m调用的方法,并且正在工作。

还有。最初,正在调用DrawRect方法,但我无法使用setNeedsUpdate手动调用。调试时,它甚至没有进入DrawRect方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-01 03:45:38

您正在创建您的DrawCircle,但您从未将其添加到视图中。

代码语言:javascript
复制
[self.view addSubview:circleView];

因此,它超出了范围,并且(如果使用ARC)会被发布到您身上。您也不需要设置它的frame,例如:

代码语言:javascript
复制
circleView = [[DrawCircle alloc] initWithFrame:self.view.bounds];

或者,您可以在接口构建器中添加视图(一个标准的UIView,但随后在IB中指定您的自定义类)。

另外,请注意,您通常甚至不调用setNeedsDisplay。将其添加到视图层次结构中将为您调用它。只有当您需要根据某些自定义属性更新视图时,才需要调用它。

就我个人而言,我倾向于这样定义drawRect

代码语言:javascript
复制
- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // I'd just use `rect` and I can then use the `frame` to coordinate location and size of the circle

    CGContextAddEllipseInRect(ctx, rect);

    // perhaps slightly simpler way of setting the color

    CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]);

    CGContextFillPath(ctx);
}

这样,圆圈的位置和大小将由我为圆圈设置的frame来决定(顺便说一句,如果这使它更加混乱,我很抱歉,但是我使用了一个不同的类名CircleView,因为我喜欢在CircleView子类的名称中使用View )。

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *circle;

    circle = [[CircleView alloc] initWithFrame:CGRectMake(100.0, 100.0, 200.0, 200.0)];
    circle.backgroundColor = [UIColor clearColor]; // because it calls `super drawRect` I can now enjoy standard UIView features like this
    [self.view addSubview:circle];

    circle = [[CircleView alloc] initWithFrame:CGRectMake(300.0, 300.0, 10.0, 10.0)];
    circle.backgroundColor = [UIColor blackColor]; // because it calls `super drawRect` I can now enjoy standard UIView features like this
    [self.view addSubview:circle];
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16312168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档