【问题标题】:draw view in context, what does it mean?在上下文中绘制视图,这是什么意思?
【发布时间】:2014-06-05 14:20:51
【问题描述】:

我有这样的视图:

_myController = [[MSCustomViewController alloc] init];
    _myController.doc = (AWDOC *)_currentDocumentViewController.activeFile.document;
    _myController.view.backgroundColor = [UIColor colorWithHue:0.7/3.3 saturation:0.2 brightness:1.0 alpha:1.0];
    [self.view insertSubview:_myController.view aboveSubview:_myController];
    CGSize size = [_myController.view sizeThatFits:CGSizeZero];
    if(size.height < _myController.frame.size.height)
        size.height = _myController.frame.size.height;
    CGRect myFrame = CGRectMake(0., CGRectGetMinY(_currentDocumentViewController.view.bounds) + (CGRectGetHeight(_currentDocumentViewController.view.bounds) - size.height) / 2., size.width, size.height);
    _myController.view.frame = myFrame;
    CGRect buttonFrame = CGRectMake(CGRectGetMaxX(myFrame), CGRectGetMinY(myFrame), myButton.frame.size.width, CGRectGetHeight(myFrame));
    _myButton.frame = buttonFrame;
    [_myButton setTitle:@"<" forState:UIControlStateNormal];

“使用上下文或 Draw 绘制此视图”是什么意思?

【问题讨论】:

  • 您能否再解释一下您是从哪里收到此消息的?
  • 这只是一项任务
  • 还是不清楚,这是错误信息吗?
  • 不,这不是错误。我只是想知道我是否可以使用一些 Draw(cgrect 等) 或 CGContext 方法来绘制视图
  • 您可以使用 CGContext 方法绘制视图,以明确“在上下文中绘制视图”参见stackoverflow.com/questions/22740543/…

标签: ios objective-c uiview draw cgcontext


【解决方案1】:

将默认 UIView 添加到您的视图控制器以保存 UIButton、UIImageView、UILabel 等内置视图。您添加为:

UIView *view = [[[UIView alloc] initwithFrame:CGRectMake(..)];

现在,要在 UIView 中制作自定义绘图,您必须定义自己的视图(您可以通过创建继承 UIView 的新类来实现)。假设您制作的课程是 StandardView 课程。 当您继承 UIView 时,drawRect: 是您在视图控制器中初始化此 uiview 时调用的默认方法:

Standardview * stndView = [[Standardview alloc]initWithFrame:CGRectMake(0, 0, 320, 300];

因此,您可以将所有绘图内容放在您的 drawRect: 方法中。通常,您使用 OpenGL/ES 或 CoreGraphics 进行绘图。在使用 coregraphics 函数时,您可以为 coregraphics 的每个函数指定上下文。而该上下文意味着您的图形目标。

【讨论】:

    猜你喜欢
    • 2014-08-12
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2011-04-25
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多