在 Objective-C (iPhone) 中设置要绘制的视图?
好吧,所以,我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你把事情想得太复杂了。不要从
drawRect
调用drawInContext
。相反,只需在您的drawRect
方法中执行此操作即可:在该代码中您应该注意一些事项。每当您通过重写drawRect在UIView中进行自定义绘图时,请使用UIGraphicsGetCurrentContext来获取对当前上下文的引用。其次,使用
CGContext
时,不需要创建新的CGPath
。CGContext
与CGPath
具有相同的绘图功能,请参阅文档 此处。要创建视图,您需要做的就是:
其中
YourView
是您的UIView
子类。您不需要创建单独的类引用 (viewClass
),也不需要在 UIView 对象上调用retain
,因为保留计数会增加当你创建它的时候就已经存在了。另请注意,CGRectZero
是一个大小为 (0, 0) 的矩形。如果您希望视图可见,则需要提供适当大小的CGRect
而不是CGRectZero
。希望这能澄清一些事情。
You're overcomplicating it. Don't call
drawInContext
fromdrawRect
. Instead just do this in yourdrawRect
method: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 usingCGContext
there is no need to create a newCGPath
.CGContext
has the same drawing functions asCGPath
, see the docs here.To create a view all you need to do is this:
Where
YourView
is yourUIView
subclass. You don't need to create a separate class reference (viewClass
), and you don't need to callretain
on the UIView object either, because the retain count is increased already when you create it. Also note thatCGRectZero
is a rectangle of size (0, 0). If you want your view to be visible then you need to supply an appropriately sizedCGRect
instead ofCGRectZero
.Hope this clarifies things a bit.