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,我是一个新手,显然,我应该学习更多