初学iphone问题:画一个矩形。我究竟做错了什么? [英] Beginner iphone question: drawing a rectangle. What am I doing wrong?

查看:118
本文介绍了初学iphone问题:画一个矩形。我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找出我在这里做错了什么。尝试了几件事,但我从未在屏幕上看到那个难以捉摸的矩形。现在,这就是我想做的 - 只需在屏幕上绘制一个矩形。

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.

除了CGContextSetRGBFillColor之外的所有内容我都得到了无效的上下文( )。在那之后得到上下文对我来说似乎有点不对,但是我不在家看昨晚我正在使用的例子。

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;

}

谢谢,

Sean。

推荐答案

从基于视图的模板开始,创建一个名为抽屉的项目。将UIView类添加到项目中。将其命名为 SquareView (.h和.m)。

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

双击 DrawerViewController.xib 将其打开 Interface Builder 。使用 Class 弹出菜单将Identity Inspector(command-4)中的通用视图更改为 SquareView 。保存并返回 Xcode

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.

将此代码放入 SquareView.m 文件的drawRect:方法中绘制一个大的,弯曲的,空的黄色矩形和一个小的绿色透明方块:

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
}

你没有为绘图调用此方法。当程序启动并激活NIB文件时,视图控制器将告诉视图至少绘制一次。

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.

这篇关于初学iphone问题:画一个矩形。我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆