开发者

Drawing a line within a view within a viewcontroller

开发者 https://www.devze.com 2023-02-28 16:42 出处:网络
So I have been searching for a solution for this a while now, and I just can\'t figure out what is wrong. Not use objectice开发者_开发知识库 c long, a few days. So, the issue is this:

So I have been searching for a solution for this a while now, and I just can't figure out what is wrong. Not use objectice开发者_开发知识库 c long, a few days. So, the issue is this:

So, I have created these: PaintingTestViewController.h PaintingTestViewController.m PaintingTestviewController.xib

I then create TestDraw.h TestDraw.m

Doubleclick on the xib file, I create a new view, select it, open the inspector, and set class to point to TestDraw.

I then open TestDraw.m and remove the comments around - void Drawrect...

Inside this method I have added

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 2.0);

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

CGFloat components[] = {0.0, 0.0, 1.0, 1.0};

CGColorRef color = CGColorCreate(colorspace, components);

CGContextSetStrokeColorWithColor(context, color);

CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 300, 400);

CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);

I have tested this before with an application which only has the view, and not the view controller. However now I get a bunch of errors

.. .. -[TestDraw drawRect:] in TestDraw.o "_CGContextStrokePath", referenced from: -[TestDraw drawRect:] in TestDraw.o "_CGContextAddLineToPoint", referenced from: -[TestDraw drawRect:] in TestDraw.o ld: symbol(s) not found

So, it seems the CGContext "methods" are not recognized.

I need to use the viewController and a separate view inside because I'm using Unity for the main application, and this will just be a small part of a bigger application. But, because of performance reasons (it's a drawing app), I need to use native code.

Ok, I hope somebody will be able to help me. Thank you so much!


Sounds like you need to add the CoreGraphics framework to your project.

How to add it will depend on which version of Xcode (3 or 4) you are using.

Xcode 4

  1. Click the project in the file listing
  2. Click the target for the application
  3. Click the 'Build Phases' tab near the top, middle of the screen
  4. Open the 'Link Binary With Libraries' option.
  5. Click the + icon
  6. Choose the CoreGraphics.framework and click 'Add'

Related question here: How to "add existing frameworks" in Xcode 4?

Once you've added the framework to your project you need to add the following to the top of your TestDraw.m file

#import <CoreGraphics/CoreGraphics.h>

0

精彩评论

暂无评论...
验证码 换一张
取 消