简体   繁体   English

iOS:如何确定何时绘制UIView?

[英]iOS: How can I decide when UIView gets drawn?

I have added a UIView on my storyboard and connected the following code to it successfully (ie the thing draws on the screen when I load the app) 我已在情节UIView板上添加了一个UIView ,并将以下代码成功连接到了它(即,在加载应用程序时,东西会绘制在屏幕上)

#import "UIView+DrawView.h"

@implementation DrawView
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}

- (void) drawRect:(CGRect)rect
{ //Get the CGContext from this view
    CGContextRef context = UIGraphicsGetCurrentContext();

    //Set the stroke (pen) color
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    //Set the width of the pen mark
    CGContextSetLineWidth(context, 5.0);

    // Draw a line
    //Start at this point
    CGContextMoveToPoint(context, 10.0, 30.0);

    //Give instructions to the CGContext
    //(move "pen" around the screen)
    CGContextAddLineToPoint(context, 310.0, 30.0);
    CGContextAddLineToPoint(context, 310.0, 90.0);
    CGContextAddLineToPoint(context, 10.0, 90.0);

    //Draw it
    CGContextStrokePath(context);
}

@end

What I would want however is to manually toggle this UIView to appear, which I cannot seem to do. 但是,我想要手动切换此UIView以显示,但我似乎做不到。 I have tried the following: 我尝试了以下方法:

  1. Setting the UIView to Hidden in Storyboard and UIView设置为在Storyboard中隐藏,然后
  2. Setting DrawView.Hidden = YES; 设置DrawView.Hidden = YES; to no avail, in both cases it draws immediately; 无济于事,在两种情况下都立即得出;

I hope that you can guide me on what I am missing here. 我希望您可以指导我了解我在这里所缺少的内容。

确保在情节提要中将“自定义类”属性设置为您的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM