简体   繁体   中英

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)

#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. I have tried the following:

  1. Setting the UIView to Hidden in Storyboard and
  2. Setting 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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