【问题标题】:UIView With Multiple DrawRect Methods具有多个 DrawRect 方法的 UIView
【发布时间】:2013-08-26 21:10:43
【问题描述】:

我目前正在尝试使用 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)

【问题讨论】:

    标签: ios drawrect cgcontext


    【解决方案1】:

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

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

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

    【讨论】:

    • 是的,这是我的工作。我制作了“setgrid/vertlines/otherstuff”方法并将它们全部添加到 ViewDidLoad。我将它们放在 If 语句中并创建 BOOL 变量来决定是否应该调用它们。我设置了那些 BOOL 以及线条粗细、颜色
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    相关资源
    最近更新 更多