1
votes

I have found tutorial which I want to implement in my view controller but the code is written in UIView so how to convert UIView code into UIViewController it can work here is my code

  #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;
   }

Only override drawRect: if you perform custom drawing. An empty implementation adversely affects performance during animation.

 - (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

      }
1
the simplest way is to add the view as subview of your view controller... also drawRect method is for view....Venk
@MountainLion can you please tell me withoud making subView how may i write this code in ViewController if i want to makeJdeveloper Iphone

1 Answers

3
votes

Create a UIViewController Object and set the view property to this view. Something like this should work.

UIViewController *vc = [[UIViewController alloc] init];
MyLineDrawingView *myView = [[MyLineDrawingView alloc] init];
vc.view = myView;