ios - 从 ViewController 实例化一个 DrawRect View

标签 ios objective-c

如果这是一个简单的问题,我深表歉意。我对使用 draw rect 还很陌生,当 draw rect 在 View 子类中时,我想从我的 View Controller 中的 ViewDidLoad 调用它。到目前为止我所要做的就是这个,但它什么也没做。我需要添加什么?谢谢

circleView = [[CircleView alloc] init];
[self.view addSubview:circleView];
[circleView setNeedsDisplay];

这是我的圈 subview 中的代码

- (void)drawRect:(CGRect)rect
{
    NSLog(@"worked");
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(contextRef, 0, 0, 255, 0.1);
    CGContextSetRGBStrokeColor(contextRef, 0, 0, 255, 0.5);

    CGContextStrokeEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25));

}

这就是我的全部。我知道可能需要用框架初始化,但我不知道如何选择绘制矩形。

最佳答案

无需在-viewDidLoad中显式调用[circleView setNeedsDisplay];,因为UIView会自动调用其 -drawRect: 第一次显示时。
您的 circleView 没有显示,因为它的 frame 是 {0, 0; 0, 0}。您可能需要给它一个有意义的 frame,如下所示:

circleView = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];

或者设置它的frame属性:

circleView.frame = CGRectMake(0, 0, 200, 200);

关于ios - 从 ViewController 实例化一个 DrawRect View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18199532/

相关文章:

ios - 从地面坠落的角色

iOS 应用程序在我的 iPhone 5s 上以 iPhone 4 分辨率运行?

ios - performFetchWithCompletionHandler 多久调用一次?

objective-c - 从另一个 View Controller 修改 View Controller 的属性

objective-c - Cocoa:在头文件中导入和在主文件中导入有什么区别?

ios - 为什么 UITableViewCell 首先显示以前的值,然后才更新自身?

ios - 如何设置 SKCameraNode 相对于节点的 x 和 y 位置?

ios - 没有名为 'AVCapturePhotoCaptureDelegate' 的类型或协议(protocol)

ios - UIView 中嵌入的动态 UITableView - 计算高度

iphone - 隐藏 UIButton 标题