Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 重写drawRect时UIBezierPath未绘制_Ios_Objective C_Cocoa Touch_Uibezierpath - Fatal编程技术网

Ios 重写drawRect时UIBezierPath未绘制

Ios 重写drawRect时UIBezierPath未绘制,ios,objective-c,cocoa-touch,uibezierpath,Ios,Objective C,Cocoa Touch,Uibezierpath,我试图在UIView子类中绘制一些东西,但当我将其添加为子视图时,它只显示一个黑色视图。我做错什么了吗 - (void)drawRect:(CGRect)rect { //// Color Declarations UIColor* shape5Color = [UIColor redColor]; //// Shape 5 Drawing UIBezierPath* shape5Path = UIBezierPath.bezierPath; [shap

我试图在UIView子类中绘制一些东西,但当我将其添加为子视图时,它只显示一个黑色视图。我做错什么了吗

- (void)drawRect:(CGRect)rect
{
    //// Color Declarations
    UIColor* shape5Color = [UIColor redColor];

    //// Shape 5 Drawing
    UIBezierPath* shape5Path = UIBezierPath.bezierPath;
    [shape5Path moveToPoint: CGPointMake(54.99, 118)];
    [shape5Path addLineToPoint: CGPointMake(460.01, 118)];
    [shape5Path addLineToPoint: CGPointMake(460.01, 355)];
    [shape5Path addLineToPoint: CGPointMake(383.75, 355)];
    [shape5Path addLineToPoint: CGPointMake(363.26, 327.94)];
    [shape5Path addLineToPoint: CGPointMake(433, 327.94)];
    [shape5Path addLineToPoint: CGPointMake(433, 145)];
    [shape5Path addLineToPoint: CGPointMake(84, 144.96)];
    [shape5Path addLineToPoint: CGPointMake(84, 327.94)];
    [shape5Path addLineToPoint: CGPointMake(152.62, 327.94)];
    [shape5Path addLineToPoint: CGPointMake(133, 355)];
    [shape5Path addLineToPoint: CGPointMake(54.99, 355)];
    [shape5Path addLineToPoint: CGPointMake(54.99, 118)];
    [shape5Path closePath];
    [shape5Path moveToPoint: CGPointMake(257, 223.5)];
    [shape5Path addLineToPoint: CGPointMake(394.27, 404)];
    [shape5Path addLineToPoint: CGPointMake(119.73, 404)];
    [shape5Path addLineToPoint: CGPointMake(257, 223.5)];
    [shape5Path closePath];

    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGFloat sx = 179.0 / shape5Path.bounds.size.width;
    CGFloat sy = 179.0 / shape5Path.bounds.size.height;

    CGContextScaleCTM(currentContext, sx, sy);
    [shape5Color setFill];
    [shape5Path fill];
}
我尝试将视图的背景设置为clearColor,但没有成功

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
     }
    return self;
}

不,我认为没有必要。从文档中可以看出:如果您直接将UIView子类化,则此方法的实现不需要调用super。但是,如果要对不同的视图类进行子类化,则应在实现中的某个点调用super。能否在drawRect中放置断点并确保调用它-可能需要为视图调用setNeedsDisplay。感谢您提供的信息:)视图的帧大小是多少?