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?)
drawRectis a method fromUIViewbut notUIViewController. You gonna need to create your customUIViewsubclass and overwritedrawRectin 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?