How to let drawRect get the value of points from V

2019-08-10 05:50发布

问题:

I created a class newView from UIView and aim to draw a quadrangle. the 4 coners (ClipPointA, ClipPointB, ClipPointC, ClipPointD) of the quadrangle are supposed to read value from ViewController.

newView.h:

@interface newView : UIView {
    CGPoint ClipPointA, ClipPointB, ClipPointC, ClipPointD; 
}
@end

newView.m:

@implementation newView

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code.
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0);    
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, ClipPointA.x, ClipPointA.y); //start point
    CGContextAddLineToPoint(context, ClipPointB.x, ClipPointB.y);
    CGContextAddLineToPoint(context, ClipPointD.x, ClipPointD.y); 
    CGContextAddLineToPoint(context, ClipPointC.x, ClipPointC.y); // end path   
    CGContextClosePath(context); // close path  
    CGContextSetLineWidth(context, 2.0); 
    CGContextStrokePath(context); 
}

- (void)dealloc {
    [super dealloc];
}

@end

I created the instance of newView in ViewController. How do I write next part of code to let 4 CGPoints read value from ViewController?

Thanks.

回答1:

when you create the new UIView please pass the points also for eg:let A,B,C,D be your 4 points

newView *nv =[[dragman alloc]initWithFrame:self.view.frame];
    nv.ClipPointA=A;
    nv.ClipPointB=B;
        nv.ClipPointC=C;
        nv.ClipPointD=D;
    [nv setUserInteractionEnabled:YES];
    [contentView addSubview:nv];
    [nv release];