iphone初學者問題:畫一個矩形。我究竟做錯了什麼? (Beginner iphone question: drawing a rectangle. What am I doing wrong?)


問題描述

iphone初學者問題:畫一個矩形。我究竟做錯了什麼? (Beginner iphone question: drawing a rectangle. What am I doing wrong?)

Trying to figure out what I'm doing wrong here.  Have tried several things, but I never see that elusive rectangle on the screen.  Right now, that's all I want to do ‑‑ just draw a single rectangle on the screen.

I'm getting an "invalid context" on everything but the CGContextSetRGBFillColor().  Getting the context after that seems kinda wrong to me, but I'm not at home looking at the examples I was using last night.

Have I messed up something else as well?  I really would like to get at least this much done tonight...

‑ (id)initWithCoder:(NSCoder *)coder
{
  CGRect myRect;
  CGPoint myPoint;
  CGSize    mySize;
  CGContextRef context;

  if((self = [super initWithCoder:coder])) {
    NSLog(@"1");
    currentColor = [UIColor redColor];
    myPoint.x = (CGFloat)100;
    myPoint.y = (CGFloat)100;
    mySize.width = (CGFloat)50;
    mySize.height = (CGFloat)50;
    NSLog(@"2");
    // UIGraphicsPushContext (context);
    NSLog(@"3");
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
    context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, currentColor.CGColor);
    CGContextAddRect(context, myRect);
    CGContextFillRect(context, myRect);
  }

  return self;

}

Thanks,

Sean.

‑‑‑‑‑

參考解法

方法 1:

Starting with a View‑based template, create a project named Drawer. Add a UIView class to your project. Name it SquareView (.h and .m).

Double‑click DrawerViewController.xib to open it in Interface Builder. Change the generic view there to SquareView in the Identity Inspector (command‑4) using the Class popup menu. Save and go back to Xcode.

Put this code in the drawRect: method of your SquareView.m file to draw a large, crooked, empty yellow rectangle and a small, green, transparent square:

‑ (void)drawRect:(CGRect)rect;
{   
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0); // yellow line

    CGContextBeginPath(context);

    CGContextMoveToPoint(context, 50.0, 50.0); //start point
    CGContextAddLineToPoint(context, 250.0, 100.0);
    CGContextAddLineToPoint(context, 250.0, 350.0);
    CGContextAddLineToPoint(context, 50.0, 350.0); // end path

    CGContextClosePath(context); // close path

    CGContextSetLineWidth(context, 8.0); // this is set from now on until you explicitly change it

    CGContextStrokePath(context); // do actual stroking

    CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.5); // green color, half transparent
    CGContextFillRect(context, CGRectMake(20.0, 250.0, 128.0, 128.0)); // a square at the bottom left‑hand corner
}

You don't have to call this method for the drawing to happen. Your view controller will tell the view to draw itself at least once when the program launches and the NIB files are activated.

方法 2:

You are not supposed to put CG code in initWithCoder. That message should only be used for INITIALIZATION purpose.

Put your drawing code in:

‑ (void)drawRect:(CGRect)rect

If you are subclassing a UIView...

(by Sean Gilleywillc2Pablo Santa Cruz)

參考文件

  1. Beginner iphone question: drawing a rectangle. What am I doing wrong? (CC BY‑SA 3.0/4.0)

#draw #quartz-graphics #iphone






相關問題

使用Java繪製維恩圖 (Draw Venn diagram using Java)

android中的ontouch:獲取X和Y坐標並在該點上繪製圓 (ontouch in android: getting X and Y coordinates and drawing circle on that point)

如何在android上將谷歌地圖圖層作為片段佈局移動 (how to move google map layers as fragment layout on android)

onTouch() 無法繪製畫布 (onTouch() can't draw canvas)

佈局僅在屏幕關閉或離開應用程序並返回後顯示兒童 (Layout only shows children after screen turn off or leaving the app and coming back)

將形狀對象的 ArrayList 繪製到框架 (Paint ArrayList of Shape Objects to Frame)

多線程圖形 (multithreading Graphics)

一次性繪製所有 DrawableGameComponents (Drawing all DrawableGameComponents in a single batch)

html5畫布繪製帶有額外數據的圓形變量 (html5 canvas draw circle variables with extra data)

iphone初學者問題:畫一個矩形。我究竟做錯了什麼? (Beginner iphone question: drawing a rectangle. What am I doing wrong?)

連接 4 Android Studio 繪製空格 (Connect 4 Android Studio Draw empty spaces)

矩形不動 (Rectangle is not moving)







留言討論