0

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

我创建了一些方法;drawVerticalLine 和 Horizo​​ntal Lines 和东西......

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

但是,我希望能够从 ViewController 单独调用它们。当我尝试这样做enter code here时。我在下面得到一个“: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)
4

1 回答 1

1

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

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

您绝不能直接调用drawRect:视图。

于 2013-08-26T21:33:53.933 回答