簡體   English   中英

在Objective-C的2點之間畫線

[英]draw line between 2 points in objective-c

我想在2點之間畫線。 但是UIGraphicsGetCurrentContext()返回null

ViewController.m

self.drawLine.firstPoint = self.btnPointOne.bounds.origin;
self.drawLine.secondPoint = self.btnPointTwo.bounds.origin;
[self.drawLine drawRect:self.drawWatchModeView.bounds];

DrawLine.h

@interface DrawLine : UIView{
    CGContextRef context;
}

@property (assign, nonatomic) CGPoint firstPoint;
@property (assign, nonatomic) CGPoint secondPoint;

DrawLine.m

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    context = UIGraphicsGetCurrentContext();
    // Drawing code
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    //line width
    CGContextSetLineWidth(context, 1.0);
    CGContextMoveToPoint(context, self.firstPoint.x, self.firstPoint.y);
    CGContextAddLineToPoint(context, self.secondPoint.x, self.secondPoint.y);
    // and now draw the Path!
    CGContextStrokePath(context);
}

但是它出錯

CGContextSetStrokeColorWithColor:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextSetLineWidth:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextMoveToPoint:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextAddLineToPoint:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextDrawPath:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextSetStrokeColorWithColor:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextSetLineWidth:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextMoveToPoint:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextAddLineToPoint:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextDrawPath:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextSetStrokeColorWithColor:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextSetLineWidth:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextMoveToPoint:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextAddLineToPoint:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextDrawPath:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextSetStrokeColorWithColor:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextSetLineWidth:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextMoveToPoint:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextAddLineToPoint:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。
CGContextDrawPath:無效的上下文0x0。 如果要查看回溯,請設置CG_CONTEXT_SHOW_BACKTRACE環境變量。

我嘗試UIViewControllerBasedStatusBarAppearanceYES它不適合我工作,我嘗試刪除UIViewControllerBasedStatusBarAppearance它不適合我

您不能只在需要時調用drawRect: ,所以您的代碼行:

[self.drawLine drawRect:self.drawWatchModeView.bounds];

需要更改以要求操作系統運行繪制周期。 發生這種情況時,將設置繪圖上下文,並且一切就緒。 當您明確運行它時,該上下文不存在。 因此,將該行更改為:

[self.drawLine setNeedsDisplay];

要么

[self.drawLine setNeedsDisplayInRect:self.drawWatchModeView.bounds];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM