Reputation: 4551
I want to draw a custom view, i am doing it like this :
#import <UIKit/UIKit.h>
@interface CustomView : UIView
/** The color used to fill the background view */
@property (nonatomic, strong) UIColor *drawingColor;
@end
#import "TocCustomView.h"
#import "UIView+ChangeSize.h"
@interface CustomView()
@property (nonatomic, strong) UIBezierPath *bookmarkPath;
@end
static CGFloat const bookmarkWidth = 20.0;
@implementation CustomView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect{
[[UIColor blueColor] setFill];
[[self bookmarkPath] fill];
}
- (UIBezierPath *)bookmarkPath{
if (_bookmarkPath) {
return _bookmarkPath;
}
UIBezierPath *aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:CGPointMake(self.width, self.y)];
[aPath moveToPoint:CGPointMake(self.width, self.height)];
[aPath moveToPoint:CGPointMake(self.width/2, self.height-bookmarkWidth)];
[aPath moveToPoint:CGPointMake(self.x, self.height)];
[aPath closePath];
return aPath;
}
@end
and i am using the view in a controller like this :
CGRect frame = CGRectMake(984, 0, 40, 243);
CustomView *view = [[CustomView alloc] initWithFrame:frame];
view.drawingColor = [UIColor redColor];
[self.view addSubview:view];
The problem that the draw rectangle is not working !! the result is a black rectangle. What i am doing wrong ?
Upvotes: 0
Views: 118
Reputation: 150565
You are creating your bezier path incorrectly. You are continuously moving to new points without adding lines. After moving to the first point you need to add lines to successive points.
Try this:
- (UIBezierPath *)bookmarkPath{
if (_bookmarkPath) {
return _bookmarkPath;
}
UIBezierPath *aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:CGPointMake(self.width, self.y)];
[aPath lineToPoint:CGPointMake(self.width, self.height)];
[aPath lineToPoint:CGPointMake(self.width/2, self.height-bookmarkWidth)];
[aPath lineToPoint:CGPointMake(self.x, self.height)];
[aPath closePath];
_bookmarkPath = aPath; // You forgot to add this as well
return aPath;
}
Upvotes: 1