Beginner iphone question: drawing a rectangle. What am I doing wrong?

邮差的信 提交于 2019-12-20 09:55:22

问题


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...



来源:https://stackoverflow.com/questions/970137/beginner-iphone-question-drawing-a-rectangle-what-am-i-doing-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!