Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.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 我的UIVIEW是通过故事板创建的,但我的drawRect无法工作_Ios_Objective C_Uiview - Fatal编程技术网

Ios 我的UIVIEW是通过故事板创建的,但我的drawRect无法工作

Ios 我的UIVIEW是通过故事板创建的,但我的drawRect无法工作,ios,objective-c,uiview,Ios,Objective C,Uiview,我想画一个圆圈。我创建了一个名为timeView的UIView子类,并将UIView的类设置为timeView。但是没有展示 #import "TimeView.h" @implementation TimeView -(void)drawRect:(CGRect)rect { CGRect bounds = self.bounds; //中心 CGPoint center;

我想画一个圆圈。我创建了一个名为
timeView
UIView
子类,并将
UIView
的类设置为
timeView
。但是没有展示

 #import "TimeView.h"

 @implementation TimeView


        -(void)drawRect:(CGRect)rect {
            CGRect bounds = self.bounds;
           //中心
            CGPoint center;
            center.x = bounds.origin.x + bounds.size.width/ 2.0;
            center.y = bounds.origin.y + bounds.size.height/ 2.0;
           //圆的半径
            float radius = (MIN(bounds.size.width, bounds.size.height));

            UIBezierPath *path = [[UIBezierPath alloc] init];

            [path addArcWithCenter:center radius:radius startAngle:0 endAngle:M_PI * 2.0
                         clockwise:YES ];

            path.lineWidth = 10;
            [[UIColor blackColor ]setStroke];
            [path stroke];
        }

        @end

获取当前绘图上下文并绘制到该上下文中

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, rect);
CGContextSetFillColor(context, CGColorGetComponents([[UIColor blackColor] CGColor]));
CGContextFillPath(context);

实际上代码是正确的,它画了一个圆。问题在于计算半径的数学逻辑。这个圆太大了,不适合那个视图

float radius = (MIN(bounds.size.width, bounds.size.height))/2;
使用以下方法计算半径,它应该有效


drawRect似乎不被称为DCG代表核心图形。我也在一个视图子类中使用它,我在其中测试了这段代码。它确实有效。如果未调用drawRect,您可能需要在控制该视图的vc中使用[TheCircledDrawingView setNeedsDisplay]来启用它。欢迎使用堆栈溢出!看起来你在请求家庭作业帮助。虽然我们对此本身没有问题,但请注意这些,并相应地编辑您的问题。hhh,我刚刚开始自学ios,我是一个新手,显然,我应该学习更多