繁体   English   中英

如何让drawRect从ViewController获得点的值并进行绘制?

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

我从UIView创建了一个newView类,旨在绘制一个四边形。 四边形的四个角(ClipPointA,ClipPointB,ClipPointC,ClipPointD)应该从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

我在ViewController中创建了newView实例。 如何编写下一部分代码,以使4个CGPoint从ViewController读取值?

谢谢。

当您创建新的UIView时,请同时传递点数,例如:让A,B,C,D为您的4点

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

暂无
暂无

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

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