Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.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 如何在UIViewController中绘制_Ios_Objective C_Uiview - Fatal编程技术网

Ios 如何在UIViewController中绘制

Ios 如何在UIViewController中绘制,ios,objective-c,uiview,Ios,Objective C,Uiview,我有一个画矩形的代码 //// General Declarations CGContextRef context = UIGraphicsGetCurrentContext(); //// Color Declarations UIColor* color = [UIColor colorWithRed: 0.48 green: 0.833 blue: 0.38 alpha: 1]; //// Rectangle Drawing CGRect rectangleRect = CGRectM

我有一个画矩形的代码

//// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();

//// Color Declarations
UIColor* color = [UIColor colorWithRed: 0.48 green: 0.833 blue: 0.38 alpha: 1];

//// Rectangle Drawing
CGRect rectangleRect = CGRectMake(58, 19, 85, 85);
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: rectangleRect];
[color setFill];
[rectanglePath fill];
{
    NSString* textContent = @"hello";
    NSMutableParagraphStyle* rectangleStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
    rectangleStyle.alignment = NSTextAlignmentCenter;

    NSDictionary* rectangleFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: @"HelveticaNeue" size: 42.5], NSForegroundColorAttributeName: UIColor.whiteColor, NSParagraphStyleAttributeName: rectangleStyle};

    CGFloat rectangleTextHeight = [textContent boundingRectWithSize: CGSizeMake(rectangleRect.size.width, INFINITY)  options: NSStringDrawingUsesLineFragmentOrigin attributes: rectangleFontAttributes context: nil].size.height;
    CGContextSaveGState(context);
    CGContextClipToRect(context, rectangleRect);
    [textContent drawInRect: CGRectMake(CGRectGetMinX(rectangleRect), CGRectGetMinY(rectangleRect) + (CGRectGetHeight(rectangleRect) - rectangleTextHeight) / 2, CGRectGetWidth(rectangleRect), rectangleTextHeight) withAttributes: rectangleFontAttributes];
    CGContextRestoreGState(context);
}
如果将代码放在uiview子类中,代码可以正常工作,但是我如何在不使用任何子类的情况下在主viewcontroller中使用它??有人可以转换代码,看看如何在viewcontroller中使用它吗


我可以将UIBezierPath放置在shapelayer中,但最后一部分是文本,我不知道如何操作。请告诉我你的问题毫无意义。这就像问你如何让你的电脑烤蛋糕

您需要仔细阅读MVC设计模式

视图控制器是控制器对象。它提供控制一组视图显示的逻辑,并在模型(数据)和构成屏幕内容的视图对象之间进行调解

视图控制器不能也不应该绘制到屏幕上。它们拥有并管理绘制到屏幕的视图对象


在封面下,视图对象使用层渲染到屏幕。您可以将图层添加到视图对象,这些图层也将被绘制。因此,是的,您可以在形状层内放置贝塞尔路径,并将该层添加到视图中,这将导致该层作为绘制视图的一部分进行绘制。

您不能“在视图控制器中”绘制。视图控制器不是视图。您需要在UIView中执行此操作。我的朋友,我可以毫无问题地将第一部分转换为CAShapeLayer。我唯一缺少的部分是文本..通过创建CAShapeLayer,您正在创建一个“可视对象”(如UIView),您正在添加到视图控制器中。你看过CATextLayer吗?谢谢你指出这一点。问题不是你是否能做到,而是你为什么想做。。。