开发者

How to let drawRect get the value of points from ViewController and draw?

开发者 https://www.devze.com 2023-02-28 06:10 出处:网络
I created a class newView fro开发者_如何学JAVAm UIView and aim to draw a quadrangle. the 4 coners (ClipPointA, ClipPointB, ClipPointC, ClipPointD) of the quadrangle are supposed to read value from Vie

I created a class newView fro开发者_如何学JAVAm 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.


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消