如何让drawRect从ViewController获得点的值并

如何让drawRect从ViewController获得点的值并

本文介绍了如何让drawRect从ViewController获得点的值并进行绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从UIView创建了一个newView类,旨在绘制一个四边形。四边形的四个角(ClipPointA,ClipPointB,ClipPointC,ClipPointD)应该从ViewController读取值。

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:

newView.h:

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

newView.m:

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

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

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

谢谢。

推荐答案

在创建新的UIView时,请通过例如:让A,B,C,D成为您的4点

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

这篇关于如何让drawRect从ViewController获得点的值并进行绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:30