簡體   English   中英

如何在iPhone中的viewdidload方法中繪制Arc

[英]how to draw Arc in viewdidload method in iPhone

我嘗試在視圖中繪制弧我使用此代碼。

- (void)viewDidLoad
{

    [super viewDidLoad];

    CGRect rect = CGRectMake(0,0,340,480);  
    UIView *ui = [[UIView alloc] initWithFrame:rect];
    [self.view addSubview:ui];
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextAddArc(context, 50, 50, 20, 0, 30, 0); 
    //set the fill or stroke color
    CGContextSetRGBFillColor(context, 0.5, 0.5, 0.5, 1.0);
    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 1.0);

    //fill or draw the path
    CGContextDrawPath(context, kCGPathStroke);
    CGContextDrawPath(context, kCGPathFill);
}

如果可能的話,在viewdidload方法中畫弧,請指導我。

您的視圖應該在請求時繪制(例如在drawRect: ,而不是在加載時視圖控制器繪制。

所以你可能想要嘗試的是創建一個UIView子類並將繪圖代碼移動到它的drawRect: .

非常簡短:

@interface MONView : UIView
@end

@implementation MONView

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

    <-- now draw here -->

}

@end

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect rect = CGRectMake(0,0,340,480);
    UIView * ui = [[MONView alloc] initWithFrame:rect];
    [self.view addSubview:ui];
    [ui release];
}

在視圖中無法繪制圓弧。

你需要做的是: -

1.)在項目中添加UIView類型的文件(比如說ArcView.hArcView.m

2.)在ArcView.m文件中實現- (void)drawRect:(CGRect)rect方法

3.)在這種方法中,您需要編寫繪制弧或任何想要繪制的邏輯的邏輯)

4.)現在,您必須在其中顯示弧的ViewController可以說( ArcViewController是您要在其中顯示弧的文件)

5.)在ArcViewController.m文件中,您需要執行以下步驟: -

a。) #import "ArcView.h"

b。)每當你想要顯示你的弧視圖時:

ArcView *vw = [[ArcView alloc] initWithFrame:*your required frame where you want to show arc*];
vw.backgroundColor=[UIColor redColor];
[self.view addSubview:vw];
[vw release];

之后,您將看到屏幕上出現弧線。

暫無
暫無

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

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