gpt4 book ai didi

ios - 调用 ViewController 外部的绘制方法

转载 作者:行者123 更新时间:2023-11-29 04:34:25 25 4
gpt4 key购买 nike

我正在尝试在外部类中设置一个方法,如下所示:

myClass.m

- (void)drawSomeStuffInContext:(CGContextRef)context atStartingPoint:(CGPoint *)coords
{
//draw some stuff (ignoring coords for now)
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 1.0f);
CGContextMoveToPoint(context, 10, 10);
CGContextAddLineToPoint(context, 100, 50);
CGContextStrokePath(context);
}

viewController.m

- (void)viewDidLoad
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGPoint startCoords = CGPointMake(100.0, 100.0);
[[myClass alloc] drawSomeStuffInContext:currentContext atStartingPoint:startCoords];
}

项目构建并运行,但我在日志中收到以下错误,但没有绘制任何内容:

[...] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
[...] <Error>: CGContextSetLineWidth: invalid context 0x0
[...] <Error>: CGContextMoveToPoint: invalid context 0x0
[...] <Error>: CGContextAddLineToPoint: invalid context 0x0
[...] <Error>: CGContextDrawPath: invalid context 0x0

我一直在网上搜索类似的问题/示例,但没有运气。是否需要不同/额外的参数?我应该从 viewDidLoad 之外的其他地方调用绘制方法吗?非常感谢您的建议!

最佳答案

drawRect 没有被调用(据我所知),这意味着 View 根本没有被绘制。你应该在drawSomeStuffInContext方法中调用类似[super initWithFrame:frame];的东西,其中frame是一个CGRect(显然)。

您也可以调用 [[myClass alloc] initWithFrame:frame];在 viewController.m 的 viewDidLoad 方法中并将起点存储在全局变量中。

关于ios - 调用 ViewController 外部的绘制方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11259931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com