0

I need to integrate PaintCode snippets into XCode, but so far I don't see the result in the simulator, here is the snippet for a rectangle:

// Rounded Rectangle Drawing
UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(45.5, 89.5, 232, 273) cornerRadius:3];
[[UIColor whiteColor] setFill];
[roundedRectanglePath fill];
[[UIColor blackColor] setStroke];
[roundedRectanglePath setLineWidth: 10];
[roundedRectanglePath stroke];

I used a void function in implementation file:

- (void) drawRect: (CGRect)rect
{
    // snippet goes here
}
Jeremiah Smith
  • 740
  • 6
  • 17
  • Was this done in some UIView subclass? Was the view part of the on-screen view hierarchy? – Phillip Mills Nov 23 '13 at 22:00
  • Not in sub-class, in view controller, but I tried sub-classing it, still doesn't work. – Jeremiah Smith Nov 23 '13 at 22:21
  • `drawRect:` works in views not in view controllers. – Phillip Mills Nov 23 '13 at 22:25
  • But I tried sub-classing it to UIView, it still did not work... – Jeremiah Smith Nov 23 '13 at 22:29
  • Can you show the code for your subclass and how you put an object of that subclass into the view hierarchy? – Phillip Mills Nov 23 '13 at 22:32
  • Just as above, but with import in view controller: #import "OnScreenGraphics.h". – Jeremiah Smith Nov 23 '13 at 22:39
  • 1
    Nothing above refers to a UIView or shows what you're doing to get the view onto the screen. And each time you mention "view controller" makes me more suspicious that you've got the code in the wrong place. :) – Phillip Mills Nov 23 '13 at 22:45
  • Importing the `UIView` subclass header file doesn't add it to the view hierarchy... You need to show where you add it as a subview to some other view. – FluffulousChimp Nov 23 '13 at 22:46
  • So how to I refer to UIView? I mean I always thought by creating another sub-class to UIView XCode automatically engages in that. I see @NSBum so I have an old code of a rectangle with CAShapeLayer, where it adds to a layer: [self.view.layer addSublayer:shapeLayer]; I think I need to adapt to that somehow... – Jeremiah Smith Nov 23 '13 at 22:47

2 Answers2

1

The PaintCode generated code needs to be wrapped in the drawRect: method of a UIView subclass.

You should also:

Import your subclass header file in the UIViewController that you seem to be working with, i.e. #import "OnScreenGraphics.h"

Create an instance of the class, e.g.

// add a subview to fill the entire view; your requirements
// may be different, of course
OnScreenGraphics *fancyView = [[OnScreenGraphics alloc] initWithFrame:self.view.bounds];
[[self view] addSubview:fancyView];

Edit 2013-11-24 07-33-25

#import <UIKit/UIKit.h>

@interface OnScreenGraphics : UIView

@end

#import "OnScreenGraphics.h"

@implementation OnScreenGraphics

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if( !self ) return nil;

    self.backgroundColor = [UIColor clearColor];

    return self;
}

- (void)drawRect:(CGRect)rect{
    UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(45.5, 89.5, 232, 273) cornerRadius:3];
    [[UIColor clearColor] setFill];
    [roundedRectanglePath fill];
    [[UIColor blackColor] setStroke];
    [roundedRectanglePath setLineWidth: 10];
    [roundedRectanglePath stroke];
}

@end

And in your view controller implementation:

@implementation AKDMoreDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    OnScreenGraphics *fancyView = [[OnScreenGraphics alloc] initWithFrame:self.view.bounds];
    [[self view] addSubview:fancyView];

}

// etc.

@end

When I execute this in a test project, I see:

rectangle-image

Now, all of this said, your highlighting subview is presumably covering the button you wish to highlight; so even if you can see the button, it may not respond to touch events unless you disable user interaction on that view fancyView.userInteractionEnabled = NO;

FluffulousChimp
  • 9,157
  • 3
  • 35
  • 42
  • Thanks @NSBum now I see the rectangle, but I can't see the rest of my app, i.e. the idea was to draw the rectangle on top. I tried with initWithFrame:CGRectMake(45.5, 89.5, 232, 273) but that just gives me a black rectangle. – Jeremiah Smith Nov 24 '13 at 11:50
  • Perhaps you could describe, or illustrate how you want the view to look. – FluffulousChimp Nov 24 '13 at 12:03
  • I want the view to have a transparently filled (clearColor I suppose) rectangle e.g. on top of a pre-existing button (one made with Interface Builder), basically just to highlight it. – Jeremiah Smith Nov 24 '13 at 12:27
  • Possibly `[[UIColor clearColor] setFill];` also set the `backgroundColor` of your custom view to clear. – FluffulousChimp Nov 24 '13 at 12:55
  • I did both clearColor [[UIColor clearColor] setFill]; and backgroundColor fancyView.layer.backgroundColor = [[UIColor clearColor] CGColor]; but it still just shows a black screen. – Jeremiah Smith Nov 24 '13 at 13:25
  • Thanks again @NSBum, that works perfectly! Now I just need to wrap my head around understanding it. The initWithFrame method creates a rectangular frame called "frame", then super is called why? Then if-clause is for code safety. – Jeremiah Smith Nov 24 '13 at 14:03
0

if you want to just do a quick display of PaintCode snippets try

https://github.com/kaishin/Verbena

inline core graphics rendered to UIImage - useful with PaintCode

brian.clear
  • 5,277
  • 2
  • 41
  • 62