0

I am trying to draw a rectangle where the user moves the finger on an iPad:

Interface of ViewController:

@interface ViewController : UIViewController {
    NSMutableArray *paths;
}

Implementation:

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInView:self.view];

        CGRect aRectangle = CGRectMake(location.x, location.y, 40, 40);
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:aRectangle];

        path = [UIBezierPath bezierPathWithOvalInRect:aRectangle];

        [paths addObject:path];       
    }
}

- (void)drawRect:(CGRect)rect {
    for (UIBezierPath *path in paths) {
        [path fill];
    }
}

but nothing will draw on screen... I thought maybe I didn't tell the view to refresh itself and call drawRect, but I tried going to iPad's home screen and re-enter the app and still nothing show?

(I was going to use an array of points, but since CGPoint is not an object that will stay, so it can't be added to the mutable array and also it will go away after the local scope ends. How can it be an array of points instead?)

Hailei
  • 42,163
  • 6
  • 44
  • 69
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • try below post for drawing...! http://stackoverflow.com/questions/10226130/draw-lines-a-touches-event-not-work-perfectly-on-fullscreen-mode-in-iphone Thanks..! – Dinesh Apr 21 '12 at 04:53

2 Answers2

3

drawRect is a method from UIView but not UIViewController. You gonna need to create your custom UIView subclass and overwrite drawRect in it, and then bind the custom view class to your view in XIB/NIB if you are using Interface Builder to build your UI structure.

Refer to iPhone: why isn't drawRect getting called?

Community
  • 1
  • 1
Hailei
  • 42,163
  • 6
  • 44
  • 69
  • how does the mutable array get passed to the UIView object? – nonopolarity Apr 21 '12 at 05:02
  • You can add `@property (retain) NSMutableArray *path` to your custom UIView subclass, and access it in `ViewController` class by `self.view.path`. – Hailei Apr 21 '12 at 05:03
  • I did choose `ViewController.xib` and chose the Custom Class to be TestView... but in `ViewController.m`, if I do `self.view.paths`, it will say "Property 'paths' not found on object of type UIView" – nonopolarity Apr 21 '12 at 05:15
  • Oops, I made a mistake. You gonna need to cast `self.view` to your custom class type and then access `paths`, or you will get warning at compile time. Sorry. – Hailei Apr 21 '12 at 05:18
  • `paths` was null all the time... I added `testView.paths = [[NSMutableArray alloc] initWithCapacity:100];` in `viewDidLoad` and I already see it getting bigger and bigger when there are Move events, but nothing is drawn – nonopolarity Apr 21 '12 at 05:33
  • I don't have your code so I am not sure... But at least you should alloc `paths` in init method of your UIView but not in your view controller. Then try to debug and set a breakpoint in `drawRect` to see whether `paths` have objects added in it? – Hailei Apr 21 '12 at 05:38
  • I think I got it, I just need to call `[self.view setNeedsDisplay];` after adding the path to the mutable array – nonopolarity Apr 21 '12 at 06:00
1

First of all, you should implement these methods in a UIView instead of a UIViewController.


Secondly, never call drawRect yourself!

I thought maybe I didn't tell the view to refresh itself and call drawRect

If you want your view to redraw the next drawing cycle/frame, call -setNeedsDisplay on it.

Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83