1

我找到了我想在我的视图控制器中实现的教程,但是代码是用编写的,UIView所以如何将UIView代码转换为UIViewController 它可以在这里工作是我的代码

  #import <UIKit/UIKit.h>


  @interface MyLineDrawingView : UIView {

  UIBezierPath *myPath;
  UIColor *brushPattern;
  }

  @end


   #import "MyLineDrawingView.h"


   @implementation MyLineDrawingView

   - (id)initWithFrame:(CGRect)frame
 {

   self = [super initWithFrame:frame];
    if (self) {
    // Initialization code

    self.backgroundColor=[UIColor whiteColor];
    myPath=[[UIBezierPath alloc]init];
    myPath.lineCapStyle=kCGLineCapRound;
    myPath.miterLimit=0;
    myPath.lineWidth=10;
    brushPattern=[UIColor redColor];
    }
   return self;
   }

drawRect:仅当您执行自定义绘图时才覆盖。空实现会对动画期间的性能产生不利影响。

 - (void)drawRect:(CGRect)rect
 {

 [brushPattern setStroke];
 [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
 // Drawing code
 //[myPath stroke];
 }

  #pragma mark - Touch Methods

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

     {
         //handle touch event

      }
4

1 回答 1

3

创建一个UIViewController对象并将视图属性设置为此视图。像这样的东西应该工作。

UIViewController *vc = [[UIViewController alloc] init];
MyLineDrawingView *myView = [[MyLineDrawingView alloc] init];
vc.view = myView;
于 2013-05-07T06:11:23.160 回答