剛開始與Core Graphics合作,我可能不知道發生了什麼。不正確地做CG圖
在下面的代碼我試圖創建一個小的圓形半透明的黑色方形覆蓋在的UINavigationController的頂部,但至今沒有露面......
UIView *notificationView = [[UIView alloc] initWithFrame:[[[self navigationController] view] frame]];
CGRect rect = CGRectMake(self.view.frame.size.width/2 - 50, self.view.frame.size.height/2, 100, 100);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
[[UIColor colorWithWhite:0 alpha:0.5] setFill];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:10];
[path fill];
[notificationView setNeedsDisplay];
[[[self navigationController] view] addSubview:notificationView];
因爲'UIGraphicsGetCurrentContext()'將返回'nil',所以這段代碼在'drawRect'之外的任何地方都會失敗。看看'UIGraphicsBeginImageContextWithOptions' – borrrden
好吧,我更新了代碼,仍然沒有顯示任何東西:/ –
你真的需要更好地瞭解所有這些東西的工作原理。我說「看看」它,而不是「只是粘貼在那裏」。這將繪製到圖像緩衝區中(但您仍然需要使用它)。不過,我看到你想讓它顯示到屏幕上。如果你想繪製到導航控制器本身,你需要繼承它並覆蓋它的'drawRect'方法。 *只有在該方法內部* UIGraphicsGetImageContext()返回非''nil'(並且你不需要在那裏開始一個圖像上下文)。 – borrrden