【问题标题】:How to let drawRect get the value of points from ViewController and draw?如何让drawRect从ViewController获取点的值并进行绘制?
【发布时间】:2011-04-25 05:39:34
【问题描述】:

我从 UIView 创建了一个类 newView 并旨在绘制一个四边形。四边形的 4 个角(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 个 CGPoints 从 ViewController 读取值?

谢谢。

【问题讨论】:

    标签: iphone drawrect viewcontroller


    【解决方案1】:

    当您创建新的 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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 2013-08-26
      • 2012-09-01
      • 1970-01-01
      相关资源
      最近更新 更多