在 Objective-C (iPhone) 中设置要绘制的视图?

发布于 2024-08-27 03:45:09 字数 1220 浏览 8 评论 0原文

好吧,所以,我对 iPhone 之类的东西都很陌生,但我什至不擅长编程,我有多年的 ActionScript 2.0 和 3.0 经验,我想设置一个视图,我也可以将变量传递给。视图将用 Quartz 绘制所有内容。

我尝试制作另一个游戏,尝试将指向 NSMutableArray 的指针添加到视图类中,但它不起作用,因为我不知道如何存储实际的类。

我想做这样的事情:

[myView setNeedsDisplay];

但我不得不做的事情

[(View*)self.view setNeedsDisplay];

最终并没有真​​正解决...

好吧,现在我得到了:

- (void) viewDidLoad {
    [super viewDidLoad];
 viewClass = [[View class] retain];
 gameView = [[[viewClass alloc] initWithFrame: CGRectZero] retain];
 [gameView setNeedsDisplay];
}

这是在我的drawInContext:context中,它是由drawRect触发的:

另外,我的drawRect确实[自drawInContext:UIGraphicsGetCurrentContext()];

CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetLineWidth(context, 3.0);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGMutablePathRef aPath = CGPathCreateMutable();
CGPathMoveToPoint(aPath, nil, 5, 5);
CGPathAddLineToPoint(aPath, nil, 45, 43);
CGContextAddPath(context, aPath);
CGContextStrokePath(context);

什么也没发生。

帮助?

哦,是的,我收到此错误::无效的上下文 每次我使用这些功能时。 :[

谢谢!

Okay, so, I'm all new to iPhone and stuff, but I'm not even at programming, I have many years of experience with ActionScript 2.0 and 3.0, I want to set up a view, that I can also pass variables to. The view is gonna draw everything with Quartz.

I tried to make another game where I tried to add a pointer to a NSMutableArray to the view class, but it didn't work cause I didn't know how to store an actual class.

I wanted to do like:

[myView setNeedsDisplay];

but I had to do

[(View*)self.view setNeedsDisplay];

didn't really work out in the end...

Okay, so now I got:

- (void) viewDidLoad {
    [super viewDidLoad];
 viewClass = [[View class] retain];
 gameView = [[[viewClass alloc] initWithFrame: CGRectZero] retain];
 [gameView setNeedsDisplay];
}

This is in my drawInContext:context, which is fired by drawRect:

Also, my drawRect does [self drawInContext: UIGraphicsGetCurrentContext()];

CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetLineWidth(context, 3.0);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGMutablePathRef aPath = CGPathCreateMutable();
CGPathMoveToPoint(aPath, nil, 5, 5);
CGPathAddLineToPoint(aPath, nil, 45, 43);
CGContextAddPath(context, aPath);
CGContextStrokePath(context);

Nothing happens.

Help?

Oh yeah, I get this error: : invalid context
for each time I use those functions. :[

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2024-09-03 03:45:09

你把事情想得太复杂了。不要从 drawRect 调用 drawInContext。相反,只需在您的 drawRect 方法中执行此操作即可:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextSetLineWidth(context, 3.0);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextMoveToPoint(context, 5, 5);
    CGContextAddLineToPoint(context, 45, 43);
    CGContextStrokePath(context);
}

在该代码中您应该注意一些事项。每当您通过重写drawRect在UIView中进行自定义绘图时,请使用UIGraphicsGetCurrentContext来获取对当前上下文的引用。其次,使用CGContext时,不需要创建新的CGPathCGContextCGPath 具有相同的绘图功能,请参阅文档 此处

要创建视图,您需要做的就是:

gameView = [[YourView alloc] initWithFrame:CGRectZero];

其中 YourView 是您的 UIView 子类。您不需要创建单独的类引用 (viewClass),也不需要在 UIView 对象上调用 retain,因为保留计数会增加当你创建它的时候就已经存在了。另请注意,CGRectZero 是一个大小为 (0, 0) 的矩形。如果您希望视图可见,则需要提供适当大小的 CGRect 而不是 CGRectZero

希望这能澄清一些事情。

You're overcomplicating it. Don't call drawInContext from drawRect. Instead just do this in your drawRect method:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextSetLineWidth(context, 3.0);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextMoveToPoint(context, 5, 5);
    CGContextAddLineToPoint(context, 45, 43);
    CGContextStrokePath(context);
}

There's a couple things you should note in that code. Whenever you do custom drawing in a UIView by overriding drawRect, use UIGraphicsGetCurrentContext to get a reference to the current context. Second, when using CGContext there is no need to create a new CGPath. CGContext has the same drawing functions as CGPath, see the docs here.

To create a view all you need to do is this:

gameView = [[YourView alloc] initWithFrame:CGRectZero];

Where YourView is your UIView subclass. You don't need to create a separate class reference (viewClass), and you don't need to call retain on the UIView object either, because the retain count is increased already when you create it. Also note that CGRectZero is a rectangle of size (0, 0). If you want your view to be visible then you need to supply an appropriately sized CGRect instead of CGRectZero.

Hope this clarifies things a bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文