UiViewcontroller.view就是UIView,,UiViewcontroller addsubview方法可以把自定义的UIView添加到UiViewcontroller里面去并展示出来。

解决方案 »

  1.   


    我的头文件中以下:   @interface testDrawVC : UIViewController
    {
         UIView *view;
    }
    对应的m文件的viewDidLoad定义view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
        view.backgroundColor =[UIColor blackColor];//黑色背景
     self.view = view; //此处self没有uiviewcontroller在m文件中定义-(void)drawRect:(CGRect)rect
    {
        CGContextRef ctx=UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(ctx, 2);
        CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);//白色线条
        const CGPoint points[]={CGPointMake(5, 45),CGPointMake(315, 245)};
        CGContextStrokeLineSegments(ctx, points, 2);
    }
    运行后在黑色背景中并没有看到白色线条。是否这种加uiview的方法并没 有执行drawRect?继续请教,这是怎么回事呢?
      

  2.   

    可以有两种方法
    第一种:自定义一个UIView
    实现一个继承自UIView的子类
    /////.h 头文件
    @interface myView:UIView @end
    ////.m 实现文件
    @implemention myView-(void)drawRect:(CGRect)rect
    {
        CGContextRef ctx=UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(ctx, 2);
        CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);//白色线条
        const CGPoint points[]={CGPointMake(5, 45),CGPointMake(315, 245)};
        CGContextStrokeLineSegments(ctx, points, 2);
    }
    @end自定义的UIView 写好以后,在你的UIViewController中导入@class myView;
     @interface testDrawVC : UIViewController
    {
         myView *_view;
    }对应的m文件的viewDidLoad定义_view = [[myView alloc] initWithFrame:[[UIScreen mainScreen]applicationFrame]];
    [self.view addSubview:_view];第二种方法:是使用CAShapeLayer+UIBezierPath 来绘制
      

  3.   

      回zhanglei5415:
      试过了,完全可以实现我的要求!讲得很详细透彻,非常佩服!
      学习了!
      结贴,给分!
      

  4.   

    可以有两种方法
    第一种:自定义一个UIView
    实现一个继承自UIView的子类