Ios 具有多个DrawRect方法的UIView
我目前正在尝试使用UIView创建网格/电影覆盖 我创造了一些方法;画垂直线和水平线等等 我有一个初始化UIGridView的UIViewController。我可以把我所有的方法放在draw rect中,然后一次将它们全部绘制出来 但是,我希望能够从ViewController单独调用它们。当我尝试执行Ios 具有多个DrawRect方法的UIView,ios,drawrect,cgcontext,Ios,Drawrect,Cgcontext,我目前正在尝试使用UIView创建网格/电影覆盖 我创造了一些方法;画垂直线和水平线等等 我有一个初始化UIGridView的UIViewController。我可以把我所有的方法放在draw rect中,然后一次将它们全部绘制出来 但是,我希望能够从ViewController单独调用它们。当我尝试执行时,在此处输入code即可。我在下面得到一个“:CGContextDrawPath:无效上下文0x0”代码。 从我的ViewController中,我希望能够调用“drawGrid:withCo
时,在此处输入code
即可。我在下面得到一个“: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)
-
(void)drawRect:(CGRect)rect
{
如果(self.verticalLinesON==是){
[自绘制VerticallinesForGrid:100:[UIColor红色]:[UIColor绿色]];
}
[self show16NineOverLay:[UIColor orangeColor]];
[self Show 4ThreeOverlay:[UIColor orangeColor]];
[自绘制水平线边框:100:[UIColor blueColor]:[UIColor yellowColor];
}
-(void)drawVerticalLinesForGrid:(float)节垂直:(UIColor*)线条颜色1:(UIColor*)线条颜色2
{
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(上下文,2);
int i=0;
浮动AmountofSectionsVerticly=SectionsVerticly;
对于(i=1;i
您应该做的是在网格视图类上设置一些状态,指定应该绘制的内容(垂直、水平等),然后在视图上调用setNeedsDisplay
这将触发对drawRect:
的调用。然后您的drawRect:
方法应查看其当前状态,并仅调用适当的方法来绘制所需的部分
决不能直接调用视图上的
drawRect:
。是的,这是我的工作。我创建了“setgrid/vertlines/otherstuff”方法,并将它们全部添加到ViewDidLoad中。我将它们放在If语句中,并创建BOOL变量来决定是否应该调用它们。我设置这些BOOL以及线的厚度、颜色