0

カスタムビューを描画したいのですが、次のようにしています:

#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

そして、私はこのようなコントローラーでビューを使用しています:

    CGRect frame = CGRectMake(984, 0, 40, 243);
    CustomView *view = [[CustomView alloc] initWithFrame:frame];
    view.drawingColor = [UIColor redColor];
    [self.view addSubview:view];

長方形の描画がうまくいかない問題!! 結果は黒い長方形です。私は何を間違っていますか?

4

1 に答える 1

1

ベジエ パスを正しく作成していません。線を追加することなく、新しいポイントに継続的に移動しています。最初のポイントに移動した後、連続するポイントに線を追加する必要があります。

これを試して:

- (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;
}
于 2013-09-17T10:01:28.217 に答える