簡體   English   中英

如何在具有核心圖形的 UIView 上繪圖?

[英]How to draw on a UIView with core graphics?

我不明白為什么我不能在 UIView 層上繪制。 那可能嗎?

我正在使用的示例代碼:

@implementation Menu
UIView *window;
UIWindow *mainWindow;
CGSize viewwidth;
    viewwidth=[[UIScreen mainScreen] bounds].size;
// viewwidth.width or viewwidth.height
//My UIView
window = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewwidth.width, viewwidth.height)];
    window.backgroundColor = [UIColor whiteColor];
    window.layer.opacity = 1.0f;
    [window setNeedsDisplay];
    [mainWindow addSubview: window];

我的繪圖方法 drawRect:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGRect rectangle = CGRectMake(0, 0, 320, 100);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.0);   //this is the transparent color
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 0.5);
    CGContextFillRect(context, rectangle);
    CGContextStrokeRect(context, rectangle);    //this will draw the border
}

我做錯了什么,問題是我得到一個空的白屏?

當您創建一個項目並選擇使用 Storyboards 時,您將在 Interface Builder 中為Main.storyboard設置一個 ViewController。

在此處輸入圖片說明

使用SceneDelegates iOS 應用程序和不使用SceneDelegates iOS 應用程序之間存在差異(iOS 13.0 之前)。 但是當應用程序加載時,您選擇的 ViewController 將被分配到您的主窗口/場景中使用。 在 AppDelegate.m(iOS 13 之前)或 SceneDelegate.m(iOS 13 開始)中,您可以直接訪問甚至將其分配給self.window.rootViewController屬性。

做繼承UIViewController它配備了基本UIView准備下與工作self.view (在里面ViewController.m本示例)你可以把更多的子視圖與財產[self.view addSubview:yourOtherView]

-(void)drawRect:(CGRect)rect是一個屬於UIView的方法,會在視圖設置更新或布局時調用。 但重要的是要知道,這不是您可以放置​​繪圖代碼的唯一方法。 當您不需要重復重繪圖形時,可以避免UIView:drawRect以降低應用程序消耗的能量影響和工作量。 當您能夠降低 CGRect 大小以在內部繪制時,工作量也會受到很大影響。

用一個詞來命名值......當值以其所屬的內容命名時,您將減少混亂的代碼。 因此,當您的項目變得更加復雜時,命名一個實際上是 UIView 的屬性“窗口”會給您帶來壓力。 因此,當window已經存在時,您創建了一個名為mainWindow的本地值。 這樣做時,您必須自己將 mainWindow 分配給應用程序 UIWindow 以使其可用。 _windowself.window已經作為 AppDelegate 或 SceneDelegate 的一部分存在時,您命名為window的 UIView 可能會讓您_window困惑。 如上所述,您有一個 UIView,它屬於您在 Storyboard 或代碼中分配的 UIViewController。

您的繪圖代碼有效。 由於您選擇追求的透明度,它可能對您不可見。

下面的一些代碼與您的 ViewController 進行比較。

#import "ViewController.h"
#import "MyGraphicView.h"

@interface ViewController ()
@property (nonatomic) MyGraphicView *graphicView;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _graphicView = [[MyGraphicView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:_graphicView];
}
@end

和一個 MyGraphicView.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyGraphicView : UIView

@end

NS_ASSUME_NONNULL_END

和相應的 MyGraphicView.h

#import "MyGraphicView.h"

@implementation MyGraphicView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
// This method is called when a view is first displayed or when an event occurs that invalidates a visible part of the view.
- (void)drawRect:(CGRect)rect {
    CGRect rectangle = CGRectMake(0, 0, 320, 100);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.0);
    CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 0.5);
    CGContextFillRect(context, rectangle);
    CGContextStrokeRect(context, rectangle);
}

@end

暫無
暫無

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

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