【问题标题】:How can i call this method in self.view?如何在 self.view 中调用此方法?
【发布时间】:2013-06-05 09:22:07
【问题描述】:

我现在正在制作这种绘制线条的方法,我想要的是用另一种方法调用它我该怎么做,请帮我解决这个问题,我该如何在 self.view 中添加它?

- (void)drawRect:(CGRect)rect
{
     CGContextRef context = UIGraphicsGetCurrentContext();

     CGContextSetLineWidth(context, 5.0);

     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

     CGFloat dashArray[] = {2,6,4,2};

     CGContextSetLineDash(context, 3, dashArray, 4);

     CGContextMoveToPoint(context, 10, 200);

     CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);

     CGContextStrokePath(context);

 }

【问题讨论】:

    标签: iphone ios objective-c drawrect


    【解决方案1】:

    将视图标记为需要重绘:

    [self.view setNeedsDisplay];
    

    reference

    【讨论】:

    • @HasnatTariq 那你没有正确解释你的问题。
    • 我想在我的 self.view 上画一条虚线 rect 帮助我怎么做这个
    • @HasnatTariq 那么drawRect 方法失败了吗?它是否在您期望的时候被调用(添加日志记录)?什么是视图子类?
    • 实际上,当我在另一个方法中调用它时,例如 '[self drawRect:CGRectMake(10,10,200,200)];]' 它在 ma view 中没有显示任何形式的 ov rect..
    • @HasnatTariq 你不能直接调用这个方法;您必须改用setNeedsDisplay(请参阅UIView 类参考中的drawRect 方法,其中说明了这一点)。
    【解决方案2】:
     [self.view setNeedsDisplay];
    

    这将调用你的 draw rect 方法。

    【讨论】:

    • 它根本不会调用drawRect 方法;它会告诉 Cocoa Touch 稍后调用它。
    猜你喜欢
    • 2011-10-31
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多