gpt4 book ai didi

ios - 具有多个 DrawRect 方法的 UIView

转载 作者:行者123 更新时间:2023-11-29 03:40:58 24 4
gpt4 key购买 nike

我目前正在尝试使用 UIView 创建网格/电影叠加层。

我创建了一些方法;绘制垂直线和水平线等等...

我有一个初始化 UIGridView 的 UIViewController。我可以将所有方法放在绘制矩形中并一次绘制它们。

但是,我希望能够从 ViewController 单独调用它们。当我尝试在此处输入代码时。我收到以下代码“:CGContextDrawPath:无效上下文0x0”。从我的 ViewController 我希望能够调用“drawGrid :withColor :andLines;”或者什么东西

    -

(void)drawRect:(CGRect)rect
{

if (self.verticalLinesON == YES) {
[self drawVerticalLinesForGrid:100 :[UIColor redColor] :[UIColor greenColor]];

}

[self show16NineOverLay:[UIColor orangeColor]];

[self show4ThreeOverLay:[UIColor orangeColor]];

[self drawHorizontalLinesForGrid:100 :[UIColor blueColor] :[UIColor yellowColor]];

}
-(void)drawVerticalLinesForGrid:(float)sectionsVertically :(UIColor *)lineColor1 :(UIColor *)lineColor2
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
int i = 0;
float amountOfSectionsVertically = sectionsVertically;
for (i = 1; i < amountOfSectionsVertically; i++)
{//Horizontal Lines first.
float xCoord = self.frame.size.width * ((i+0.0f)/amountOfSectionsVertically);
CGContextMoveToPoint(context, xCoord, 0);
CGContextAddLineToPoint(context, xCoord, self.frame.size.height);
if (i%2 == 1)
{//if Odd
CGContextSetStrokeColorWithColor(context, lineColor1.CGColor);
}
else if(i%2 == 0)
{//if Even
CGContextSetStrokeColorWithColor(context, lineColor2.CGColor);
}
CGContextStrokePath(context);
}
}
-(void)drawHorizontalLinesForGrid :(float)sectionsHorizontally :(UIColor *)lineColor1 :(UIColor *)lineColor2
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
int i = 0;
float amountOfSectionsHorizontally = sectionsHorizontally;
for (i = 1; i < amountOfSectionsHorizontally; i++)
{//Vertical Lines first.
float yCoord = self.frame.size.height * ((i+0.0f)/amountOfSectionsHorizontally);
CGContextMoveToPoint(context, 0, yCoord);
CGContextAddLineToPoint(context, self.frame.size.width, yCoord);
if (i%2 == 1)
{//if Odd
CGContextSetStrokeColorWithColor(context, lineColor1.CGColor);
}
else if(i%2 == 0)
{//if Even
CGContextSetStrokeColorWithColor(context, lineColor2.CGColor);
}
CGContextStrokePath(context);
}
}
-(void)show16NineOverLay:(UIColor *)lineColor
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10);
//x/y
float yCoord = (0.5) * (self.frame.size.height * (1.778)

最佳答案

您应该做的是在 GridView 类上设置一些状态,指定应绘制的内容(仅垂直、仅水平、两者等),然后在 View 上调用 setNeedsDisplay

这将触发对drawRect:的调用。然后,您的 drawRect: 方法应该查看其当前状态并调用适当的方法来绘制所需的部分。

您绝不能直接在 View 上调用drawRect:

关于ios - 具有多个 DrawRect 方法的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18453070/

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